繁体   English   中英

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

[英]java sending email with activation link

我想我有一个很简单的问题。 我正在使用Java和glassfish服务器在Web应用程序上工作,用户可以在其中注册,注册后,我想向他们发送一封带有激活链接的电子邮件。

是否可以在不使用外部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();
      }
   }
}

是的,你可以做。

只需调用此功能即可向客户端发送自动电子邮件。 在参数“收件人”中,是您要将电子邮件发送到的电子邮件地址。

有关附加pdf的信息,请参考本教程

我通常在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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM