java - 在jsp中的XAMPP中使用mercury服务器发送邮件到gmail

标签 java jsp email

我需要从 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/

相关文章:

java - ArrayList 中的自定义对象和可搜索对象

perl - 安装 Net::SMTP::SSL

ios - 如何快速验证电子邮件地址?

java - 类型不匹配 : cannot convert from <ObjectTypeX> to <ObjectTypeX> in Tomcat7/Java 1. 7

java - 无法使用JSP和TOMCAT 7连接MYSQL数据库

java - 如何在 Spring 和 Hibernate 中保存当前用户的信息?

java - 使用 Mule 发送格式化邮件

java - 更新adt后出错

java - 启用单个 h :selectbooleancheckbox from many inside rich:datatable in JSF

java - 不是声明