我正在使用我的Gmail帐户运行这个简单的示例,但它不起作用并给出以下错误:
send failed, exception: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. nv2sm4478384pbb.6
Run Code Online (Sandbox Code Playgroud)
这是我的代码
public class Email
{
public static void main(String [] args)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.googlemail.com");
props.put("mail.from", "myemail@gmail.com");
Session session = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
"myemail@hotmail.com");
msg.setSubject("JavaMail hello world example");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("send failed, exception: " + mex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 19
必须更改smtp port和socketFactory
String to = "reciveremail@xxxx.xxx";
String subject = "subject"
String msg ="email text...."
final String from ="senderemail@gmail.com"
final String password ="senderPassword"
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//session.setDebug(true);
Transport transport = session.getTransport();
InternetAddress addressFrom = new InternetAddress(from);
MimeMessage message = new MimeMessage(session);
message.setSender(addressFrom);
message.setSubject(subject);
message.setContent(msg, "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
transport.connect();
Transport.send(message);
transport.close();
}
Run Code Online (Sandbox Code Playgroud)
希望它对你有用..
Vas*_*nan 10
props.put("mail.smtp.starttls.enable", "true");
Run Code Online (Sandbox Code Playgroud)
解决了我的问题;)
我的问题是:
com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令.u186sm7971862pfu.82 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.example.sendmail.SendEmailExample2.main(SendEmailExample2.java:53)
Run Code Online (Sandbox Code Playgroud)
我在构建电子邮件通知应用程序时也遇到了同样的问题。你只需要添加一行。下面的一个拯救了我的一天。
props.put("mail.smtp.starttls.enable", "true");
Run Code Online (Sandbox Code Playgroud)
com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0 必须首先发出 STARTTLS 命令。h13-v6sm10627790pgp.13 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.smruti.email.EmailProject.EmailSend.main(EmailSend.java:99)
Run Code Online (Sandbox Code Playgroud)
希望这对您有帮助。
| 归档时间: |
|
| 查看次数: |
118197 次 |
| 最近记录: |