【问题标题】:Sending a mail to gmail using mercury server in XAMPP in jsp在 jsp 中使用 XAMPP 中的水星服务器向 gmail 发送邮件
【发布时间】:2017-06-21 16:23:21
【问题描述】:

我需要从 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();}  
}  
} 

有人请纠正我。

【问题讨论】:

  • 这只是java代码,我稍后会转换成jsp/servlet

标签: java jsp email


【解决方案1】:

将您的 smpt 服务器配置更改为此并使用 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);

【讨论】:

  • 谢谢@FrAn,代码工作正常......我收到了邮件。
猜你喜欢
  • 2015-05-24
  • 2012-06-08
  • 2012-08-29
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 2017-06-24
  • 2016-09-07
  • 2017-08-11
相关资源
最近更新 更多