【发布时间】: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