簡體   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