[英]SMTP exception in sending the email through java code
我正在嘗試使用Java發送電子郵件,但是我的代碼在此處引發了異常Exceptionjavax.mail.MessagingException:無法將命令發送到SMTP主機;
我正在使用以下代碼
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail {
public static void main(String args[]) {
final String username ="mymail@gmail.com";
final String password = "password here";
final String xtomail="email here";
final String cc="email here";
final String bcc="email here";
final String xsub="heavy discount for static websites";
final String xbody="we are great leader in software industry";
Properties props = new Properties();
try{
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
} catch(Exception ex) {
System.out.println("exception here"+ex);
}
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(xtomail));
message.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
message.addRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
message.setSubject(xsub);
message.setText(xbody);
Transport.send(message);
System.out.println("email send");
}
catch (Exception e) {
System.out.println("Exception here"+e);
}
}
}
調試輸出顯示什么?
您的Gmail帳戶是否啟用了安全性較低的應用程序 ?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.