java发送带激活链接的电子邮件

时间:2016-03-18 10:29:29

标签: java email glassfish

我想我有一个非常简单的问题。我正在开发一个带有java和glassfish服务器的网络应用程序,用户可以注册,在他们这样做之后,我想给他们发送一封带有激活链接的电子邮件。

是否可以在没有外部smtp服务器的情况下使用java邮件API发送邮件?

由于用户无需回复该邮件。我似乎缺乏发送电子邮件的基本知识。我只想发明一些发件人地址,如“registration@onlineshop.com”。对我来说很明显,我需要一个该域的邮件服务器,以便可以向该地址发送消息。但是,如果我只是从该地址发送邮件,为什么我不能发明地址?

我不想使用google或yahoo等外部服务。如果不可能,你可以建议我一个与glassfish一起使用的开源邮件服务器吗?我的意思是,是否可以将glassfish用作电子邮件服务器?如果没有,我还能用什么呢?

谢谢!

2 个答案:

答案 0 :(得分:0)

是的,你可以这样做。只需使用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();
      }
   }
}

答案 1 :(得分:0)

是的,你可以做到。

只需调用此函数即可向客户端发送自动电子邮件。 在参数&#34;到&#34;是您要向其发送电子邮件的电子邮件地址。

要附加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);
    }
}
相关问题