java 邮件 API 的传输功能无法正常工作
java mail API's Transport function not working
我尝试 smtp.gmail.com 使用 java API 发送邮件,当我使用 Transport.send(Mimemessage) 时它显示错误,用户名和密码未被接受。
我已经按照这个link做了所有的事情,它仍然没有用。
然后,我尝试使用 SmtpTransport,它有效。
我的问题是,为什么 Transport.send() 不起作用以及 SmtpTransport 是如何工作的。
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Email {
Email(String from, String pwd, String to, String sub, String msg) {
System.out.println("Entered");
Properties prop = new Properties();
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.user", from); // User name
prop.put("mail.smtp.password", pwd); // password
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
Authenticator a = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("from", "pwd");
}
};
System.out.println("Authenticating");
Session ses = Session.getDefaultInstance(prop, a);
System.out.println("Obtained sesion");
System.out.println(ses);
try {
MimeMessage m = new MimeMessage(ses);
m.setFrom(new InternetAddress(from));
m.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
m.setSubject(sub);
m.setText(msg);
System.out.println("Message constructed");
Transport.send(m); // / This method causes error
System.out.println("transported");
System.out.println("send successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Email("xxxxxxxxxxxxxxxx@gmail.com", "xxxxxx", "xxxxx@gmail.com",
"JavaMail", "Firstmail");
}
}
您将 from
和 pwd
硬编码到 PasswordAuthentication 构造函数中是错误的。只需删除硬编码值并将 from
和 pwd
传递到方法中即可。
我已经根据下面的代码对您的 class 进行了更正。
public class Email {
Email(final String from,final String pwd,String to,String sub,String msg){
....
Authenticator a=new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(from,pwd);
}
};
...
}
我尝试 smtp.gmail.com 使用 java API 发送邮件,当我使用 Transport.send(Mimemessage) 时它显示错误,用户名和密码未被接受。
我已经按照这个link做了所有的事情,它仍然没有用。
然后,我尝试使用 SmtpTransport,它有效。
我的问题是,为什么 Transport.send() 不起作用以及 SmtpTransport 是如何工作的。
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Email {
Email(String from, String pwd, String to, String sub, String msg) {
System.out.println("Entered");
Properties prop = new Properties();
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.user", from); // User name
prop.put("mail.smtp.password", pwd); // password
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
Authenticator a = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("from", "pwd");
}
};
System.out.println("Authenticating");
Session ses = Session.getDefaultInstance(prop, a);
System.out.println("Obtained sesion");
System.out.println(ses);
try {
MimeMessage m = new MimeMessage(ses);
m.setFrom(new InternetAddress(from));
m.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
m.setSubject(sub);
m.setText(msg);
System.out.println("Message constructed");
Transport.send(m); // / This method causes error
System.out.println("transported");
System.out.println("send successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Email("xxxxxxxxxxxxxxxx@gmail.com", "xxxxxx", "xxxxx@gmail.com",
"JavaMail", "Firstmail");
}
}
您将 from
和 pwd
硬编码到 PasswordAuthentication 构造函数中是错误的。只需删除硬编码值并将 from
和 pwd
传递到方法中即可。
我已经根据下面的代码对您的 class 进行了更正。
public class Email {
Email(final String from,final String pwd,String to,String sub,String msg){
....
Authenticator a=new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(from,pwd);
}
};
...
}