我一直在尝试从我的应用程序(Java Web)发送电子邮件,但它并没有为我工作。我正在使用Microsoft Excange Server作为我的主机和端口25,但我一直得到以下内容:
Caused by: java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated
at com.mimacs.scheduler.SendEmailImpl.execute(SendEmailImpl.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at com.mimacs.scheduler.SendEmailImpl.execute(SendEmailImpl.java:72)
... 9 more
我的代码如下:
public void sendEmail() {
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", "myHost");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", debug);
props.put("mail.smtp.port", 25);
props.put("mail.smtp.starttls.enable", debug);
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myusername@example.com", "mypassword");});
session.setDebug(debug);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myusername@example.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("someone@example.com"));
message.setSubject("Testing Subject");
message.setText("Sending email through Microsoft Exchange Server");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
愿你帮助我指出我哪里出错了。我可以使用相同的电子邮件地址和登录凭据从我的Outlook发送电子邮件。但是当涉及到我的应用程序时,它给了我这个错误。