我是java邮件的新手,我想发送带附件的邮件,所以我想用互联网上的示例代码创建一个测试邮件但我得到了
javax.mail.sendfailedexception: sending failed;
nested exception is: class javax.mail.authenticationfailedexception
at javax.mail.transport.send0(Transport.java.218)
at javax.mail.transport.send(Transport.java.80)
我尝试了不同的身份验证,但我失败了
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "hari@gmail.com";
// Sender's email My Office mail server
String from = "hari8750@access.co.in";
String pass = "Password";
String host = "172.23.5.10";
String port = "25";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
properties.setProperty("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getInstance(properties,new MailAuthentication(from,pass));
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("Test Mail");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Test Mail Success Hari");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
import javax.mail.*
public class MailAuthentication extends Authentication
{
String _user;
String _pass;
public GMailAuthenticator (String username, String password)
{
super();
this._user = username;
this._pass = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(_user, _pass);
}
}
我也试过
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
和
Session session = Session.getDefaultInstance(properties);
和
Session session = Session.getDefaultInstance(properties);
但我得到同样的错误。
请帮我解决这个问题
我正在使用Windows PC,就像java环境变量一样,对于SMTP有什么步骤吗?
先谢谢。
答案 0 :(得分:0)
您必须添加mail.smtp.ssl
和mail.smtp.sender.address
属性..
mail.smtp.sender.address
与from
属性相同。
它必须工作......我正在按预期工作..
希望它有所帮助
如果它没有解决您的问题,只需共享由...引起的异常堆栈跟踪。
答案 1 :(得分:-1)
您可以切换到Google制作的应用程序(例如Gmail)来访问您的帐户(推荐)或更改settings以便account is no longer protected by modern security standards。