如何在spring mvc中向特定的电子邮件ID发送电子邮件
Posted
技术标签:
【中文标题】如何在spring mvc中向特定的电子邮件ID发送电子邮件【英文标题】:How can I send an email to a particular email id in spring mvc 【发布时间】:2016-11-06 21:53:35 【问题描述】:This is my web page.How I can send my all details to a particular email id
【问题讨论】:
欢迎来到堆栈溢出,请阅读***.com/help/how-to-ask 并改进您的问题。 【参考方案1】: public static void main(String[] args)
String to="Enter email id to whom you want to send your email";//change accordingly
//Get the session object
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("Your login id","Your login password");//change accordingly
);
//compose message
try
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("token.verify@gmail.com"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");
//send message
Transport.send(message);
System.out.println("message sent successfully");
catch (MessagingException e) throw new RuntimeException(e);
上面的代码将帮助您向特定的电子邮件 ID 发送电子邮件..
【讨论】:
以上是关于如何在spring mvc中向特定的电子邮件ID发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章