我完成了向最终用户发送邮件。但是当我当时使用 gmail 帐户时,我必须将 smtp.domain.com 更改为 smtp.gmail.com,如果我从 yahoomail.com 发送邮件,那么我必须将 smtp.gmail.com 更改为 smtp.mail。雅虎网。那么java中有没有任何解决方案可以从任何域用户发送邮件,这意味着我想让我的代码通用。所以任何人都有解决方案然后请向我提出建议。我的代码在下面
enter code here
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xyz.123@gmail.com", "123456");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xyz.123@gmail.com"));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress("abc.456@gmail.com"));
message.setSubject("mail to user");
message.setText("Hello world.....");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}