是否可以使用GMail帐户从我的Java应用程序发送电子邮件?我已经使用Java应用程序配置我的公司邮件服务器来发送电子邮件,但是当我分发应用程序时,这不会削减它.任何使用Hotmail,Yahoo或GMail的答案都是可以接受的.
我正在使用我的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)