我需要从 XAMPP 中的本地邮件服务器 Mercury 向 gmail 发送邮件。我配置了它。我编写了一个 java 程序来使用 JavaMail API 发送邮件。当我运行它时,它显示它已发送。但是,我没有收不到 Gmail 中的任何邮件。
以下是从互联网上获取的代码
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMailBySite {
public static void main(String[] args) {
String host="127.0.0.1";
final String user="root@localhost.com";//change accordingly
final String password="root";//change accordingly
String to="kishorejohnsan.s@gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("javatpoint");
message.setText("This is simple program of sending email using JavaMail API");
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
} catch (MessagingException e) {e.printStackTrace();}
}
}
请大家指正。
最佳答案
将您的 smtp 服务器配置更改为此并使用 Google 的 gmail smtp 服务器:
String host = "smtp.gmail.com";
final String user="kishorejohnsan.s@gmail.com";
final String password="your gmail account password";//change accordingly
String to = "kishorejohnsan.s@gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", 587);
关于java - 在jsp中的XAMPP中使用mercury服务器发送邮件到gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42038783/