这仅适用于(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;
}
答案 0 :(得分:5)
我觉得你这里有问题。你认证是空的。
Authenticator auth = new SMTPAuthenticator();
尝试
Authenticator auth = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
}
};
此代码适用于我。 javax.mail v1.5
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
final String smtpAuthUserName = "YOUR_ADDRES@YOURCOMPANY.COM";
final String smtpAuthPassword = "YOUR_PASSWORD";
String emailFrom = "EMAIL_FROM";
String emailTo = "EMAIL_TO";
Authenticator authenticator = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtpAuthUserName, smtpAuthPassword);
}
};
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", "smtp.office365.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance( properties, authenticator );
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailFrom));
InternetAddress[] to = {new InternetAddress(emailTo)};
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject("PLACE_SUBJECT_HERE");
message.setText("YOUR_MESSAGE_HERE");
Transport.send(message);
}
catch (MessagingException exception)
{
exception.printStackTrace();
}
}
}
答案 1 :(得分:0)
如果在添加上述代码后错误仍然存在。请参阅项目的mail.jar。可能是该项目的mail.jar已损坏。使用最新版本的mail.jar更改它并检查< / p>
答案 2 :(得分:0)
使用较旧版本的 javax.mail (1.3.1)时出现此错误。当我切换到更高版本(1.4.4)时,问题就消失了。
如果您正在使用Eclipse,则可能需要在构建路径中查找项目正在使用的库,而不仅仅是您指定的外部库,还有内置的JavaEE5等可能具有的库。其中的这个库的旧版本。
您可以在代码中查看此语句上调试所使用的库的确切版本:Session session = Session.getInstance(...
它不会向您显示源代码,但会为您提供它正在使用的mail.jar的目录路径,您可以打开它以查看版本(在 MANIFEST中文件)。