这仅适用于(smtp.office365.com)SMTP。
public int sendEmail(String fromName,String fromAddress,ArrayList toAddressList ,ArrayList ccAddressList,ArrayList bccAddressList,String subject,String message,String SmtpServerIP,String smtpUserName,String smtpPassword, ArrayList<String> attachmentFilePath ){
SMTP_HOST_NAME=SmtpServerIP;
SMTP_AUTH_USER=smtpUserName;
SMTP_AUTH_PWD=smtpPassword;
String emailmultipart="true";
String smtpHost=SmtpServerIP;
//System.out.println("SmtpServerIP"+SmtpServerIP);
System.out.println("fromName :"+fromName+":SmtpServerIP:"+SmtpServerIP+":smtpPassword:"+smtpPassword+":smtpUserName:"+smtpUserName);
boolean debug = false;
Properties props = new Properties();
props.put("mail.smtp.starttls.enable","true");
props.setProperty("mail.transport.protocol", "smtp");
if(smtpHost==null){
return -99;
}
if(smtpHost.length()>0){
//props.put("mail.smtp.host", smtpHost);
props.setProperty("mail.host", smtpHost);
}
else
{
return 1;
}//Error No SmtpHost name Found.
/* if(smtpUserName!=null && smtpUserName.length()>0){
props.setProperty("mail.user", smtpUserName);
}
if(smtpPassword!=null && smtpPassword.length()>0){
props.setProperty("mail.password", smtpPassword);
}*/
Session session=null;
if(smtpUserName!=null && smtpUserName.length()>0&&smtpPassword!=null && smtpPassword.length()>0){
System.out.println("smtpUserName111111111111111"+smtpUserName);
props.put("mail.smtp.auth","true");
Authenticator auth = new SMTPAuthenticator();
session = Session.getDefaultInstance(props, auth);
}else{
System.out.println("smtpUserName2222222222222222"+smtpUserName);
session = Session.getDefaultInstance(props, null);
}
session.setDebug(debug);
try{
//System.out.println("toAddressList.size()"+toAddressList.size());
Message msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(fromAddress,fromName);
String msgSubject=subject;
msg.setFrom(from);
msg.setSubject(msgSubject);
msg.setContent(message, "text/html; charset=utf-8");
//msg.setSentDate(new Date(2005,9,12));
//for adding TO address list.
if(toAddressList.size()>0){
Address toAddresses[]= new InternetAddress[toAddressList.size()];
toAddresses=getAddresses(toAddressList);
//System.out.println(" toAddresses |||"+toAddresses);
msg.setRecipients(Message.RecipientType.TO,toAddresses);
}
else{
return 2;
}
//for adding CC address list.
if(ccAddressList.size()>0){
Address ccAddresses[]= new InternetAddress[ccAddressList.size()];
ccAddresses=getAddresses(ccAddressList);
msg.setRecipients(Message.RecipientType.CC,ccAddresses);
}
//for adding BCC address list.
if(bccAddressList.size()>0){
Address bccAddresses[]= new InternetAddress[bccAddressList.size()];
bccAddresses=getAddresses(bccAddressList);
msg.setRecipients(Message.RecipientType.BCC,bccAddresses);
}
if(attachmentFilePath.size()>0){
//System.out.println("Inside File Attachment attachmentFilePath.length()"+attachmentFilePath.length());
//File file=new File(attachmentFilePath);
//if(file.exists()){
msg=attachFileAndMessage(msg,attachmentFilePath,message);
//}else{
// System.out.println("File does Not exists.");
// return 3;
//}
}
// Transport.send(msg);
msg.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
//System.out.println("smtpHost :: "+smtpHost+" smtpUserName ::"+smtpUserName+" smtpPassword ::"+smtpPassword);
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
catch(MessagingException mex){
mex.printStackTrace();
String errmsg=mex.getMessage().toString();
if(errmsg.indexOf("Unknown SMTP host")>-1){
System.out.println("Invalid SMTP server entry");
return 101;
}else if(errmsg.indexOf("Invalid Addresses")>-1){
System.out.println("Invalid Address entry");
return 102;
}
System.out.println("Error 1 ::"+mex.getMessage());
mex.printStackTrace();
return 4;
}
catch(Exception e){
System.out.println("Error 2 ::"+e.getMessage());
e.printStackTrace();
return 5;
}
return 0;
}