[英]JavaMail - multiple senders
I have a simple javamail class to send emails through SMTP.我有一个简单的 javamail 类来通过 SMTP 发送电子邮件。 It works as long as I only send emails from one address.
只要我只从一个地址发送电子邮件,它就可以工作。 If I try using another address, it raises this exception for some reason:
如果我尝试使用另一个地址,它会出于某种原因引发此异常:
com.sun.mail.smtp.SMTPSendFailedException: 550 5.1.0 Use your own address, please.
Here's my class:这是我的课:
public class EmailSender {
private static final String HOST = "xxxx.xxxxxx.xx";
private static final String PORT = "xx";
public static boolean sendMail(String from, String to, String pass, String subject, String text) {
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", HOST);
properties.setProperty("mail.smtp.port", PORT);
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.user", from);
properties.setProperty("mail.password", pass);
Session session = Session.getDefaultInstance(properties, new CustomAuthenticator(from, pass));
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
Transport.send(message);
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
}
In case you're wondering, the CustomAuthenticator class looks like this:如果您想知道,CustomAuthenticator 类如下所示:
public class CustomAuthenticator extends Authenticator {
private String user;
private String pw;
public CustomAuthenticator(String username, String password) {
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pw);
}
}
So I found out that the solution is to use the SMTPTransport class.所以我发现解决方案是使用 SMTPTransport 类。
public class EmailSender {
private static final String HOST = "xxxx.xxxxxx.xx";
private static final String PORT = "xx";
public static boolean sendMail(String from, String to, String pass, String subject, String text) {
Properties properties = new Properties();
properties.setProperty("mail.smtp.port", PORT);
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties, new CustomAuthenticator(from, pass));
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
SMTPTransport tp = (SMTPTransport) session.getTransport();
tp.connect(HOST, from, pass);
tp.sendMessage(message, message.getAllRecipients());
tp.close();
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
}
Your server won't let you use an address that's not yours.您的服务器不会让您使用不属于您的地址。 Depending on your server, there may be a way to convince it that the other address is also yours.
根据您的服务器,可能有一种方法可以说服它另一个地址也是您的。 Normally "yours" means "the address associated with the account you used when you logged in".
通常“您的”是指“与您登录时使用的帐户相关联的地址”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.