java 发送激活邮件 link
java sending email with activation link
我想我有一个很简单的问题。我正在开发一个带有 java 和 glassfish 服务器的网络应用程序,用户可以在其中注册,在他们注册之后,我想向他们发送一封包含激活 link.
的电子邮件
是否可以在没有外部 smtp 服务器的情况下使用 java 邮件 API 发送邮件?
因为用户没有必要回复那封邮件。看来我缺乏发送电子邮件的基本知识。我只想发明一些发件人地址,例如 "registration@onlineshop.com"。对我来说很明显,我需要一个用于该域的邮件服务器,以便可以将消息发送到该地址。但是,如果我只是从那个地址发送邮件,为什么我不能发明这个地址呢?
我不想使用 google 或 yahoo 等外部服务。如果不可能,你能建议我一个与 glassfish 一起使用的开源邮件服务器吗?我的意思是,是否可以将 glassfish 用作电子邮件服务器?如果没有,我还能用什么?
谢谢!
是的,你可以做到。只需使用 javax 邮件库即可。
如果你使用 Maven,你会做这样的事情
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
那么你可以这样做
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
String from = "registration@onlineshop.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "localhost");
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Registration from me :)");
message.setText("You got yourself an account. congrats");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
是的,你可以做到。
只需调用此函数即可向客户发送一封自动电子邮件。
在参数 "to" 中是您要向其发送电子邮件的电子邮件地址。
如需附加 pdf,请参阅 this tutorial。
我一般在Maven项目中做。如果您使用的是 Maven 项目,则导入以下依赖项。
https://mvnrepository.com/artifact/javax.mail/mail/1.4
private void sendMail(String to, String subject, String emailBody) throws MessagingException{
final String username = "youremail@gmail.com";
final String password = "emailPassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("shubham20.yeole@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setContent(emailBody, "text/html; charset=utf-8");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
我想我有一个很简单的问题。我正在开发一个带有 java 和 glassfish 服务器的网络应用程序,用户可以在其中注册,在他们注册之后,我想向他们发送一封包含激活 link.
的电子邮件是否可以在没有外部 smtp 服务器的情况下使用 java 邮件 API 发送邮件?
因为用户没有必要回复那封邮件。看来我缺乏发送电子邮件的基本知识。我只想发明一些发件人地址,例如 "registration@onlineshop.com"。对我来说很明显,我需要一个用于该域的邮件服务器,以便可以将消息发送到该地址。但是,如果我只是从那个地址发送邮件,为什么我不能发明这个地址呢?
我不想使用 google 或 yahoo 等外部服务。如果不可能,你能建议我一个与 glassfish 一起使用的开源邮件服务器吗?我的意思是,是否可以将 glassfish 用作电子邮件服务器?如果没有,我还能用什么?
谢谢!
是的,你可以做到。只需使用 javax 邮件库即可。
如果你使用 Maven,你会做这样的事情
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
那么你可以这样做
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
String from = "registration@onlineshop.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "localhost");
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Registration from me :)");
message.setText("You got yourself an account. congrats");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
是的,你可以做到。
只需调用此函数即可向客户发送一封自动电子邮件。 在参数 "to" 中是您要向其发送电子邮件的电子邮件地址。
如需附加 pdf,请参阅 this tutorial。
我一般在Maven项目中做。如果您使用的是 Maven 项目,则导入以下依赖项。
https://mvnrepository.com/artifact/javax.mail/mail/1.4
private void sendMail(String to, String subject, String emailBody) throws MessagingException{
final String username = "youremail@gmail.com";
final String password = "emailPassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("shubham20.yeole@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setContent(emailBody, "text/html; charset=utf-8");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}