即使在尝试使用各种端口(587,465和25),SMTP服务器(smtp.gmail.com,smtp.live.com和smtp.mail.yahoo.com)之后,重定向退回的电子邮件对我也无效。 ),消息对象(MimeMessage,Message和SMTPMessage)和JavaMail API方法(message.addFrom,message,setReplyTo和message.setHeader)。我还尝试更改javax.mail jar版本1.4,1.5等等。他们说堆栈流中的任何地方,将其他电子邮件设置为mail.smtp.from应该可行。但我所有退回的电子邮件都会回复给发件人。不是指定的电子邮件地址。有人可以帮我将其重定向到新的电子邮件地址吗? 这是我正在使用的代码
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import com.sun.mail.smtp.SMTPMessage;
public class SendEmail {
public static void main(String[] args) throws Exception {
Properties properties=new Properties();
InputStream input=new FileInputStream("SendEmail.properties");
properties.load(input);
// String smtpServer = "smtp.gmail.com";
String smtpServer = "smtp.live.com";
int port = 587;
final String userid = "myemail@hotmail.com";//change accordingly
final String password = properties.getProperty("EMAIL_PASSWORD1");
String contentType = "text/html";
String subject = "test: bounce an email to a different address " +
"from the sender";
String to = "bounceee@fauxmail.com";//some invalid address
String bounceAddr = "bounceremail@gmail.com";//change accordingly
String body = "Test: get message to bounce to a separate email address";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.from", bounceAddr);
Session mailSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userid, password);
}
});
// MimeMessage message = new MimeMessage(mailSession);
SMTPMessage message=new SMTPMessage(mailSession);
message.addFrom(InternetAddress.parse(userid));
message.setRecipients(Message.RecipientType.TO, to);
message.setHeader("Return-path", bounceAddr);
message.setSubject(subject);
message.setContent(body, contentType);
Transport transport = mailSession.getTransport();
try {
System.out.println("Sending ....");
transport.connect(smtpServer, port, userid, password);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
System.out.println("Sending done ...");
} catch (Exception e) {
System.err.println("Error Sending: ");
e.printStackTrace();
}
transport.close();
}// end function main()
}
答案 0 :(得分:0)
公共服务器不太可能允许您将信封从地址设置为他们不知道您拥有的地址,以防止您以退回邮件的形式间接发送垃圾邮件。如果服务器不允许您将其用作常规发件人地址,则您可能无法将其用作来自地址的信封。对于Gmail,this page会告诉您如何使用其他地址。
请注意,您可以通过启用JavaMail debug output并查找MAIL FROM行来查看正在使用的地址信封。