2016-03-18 121 views
0

我想我有一个很简单的问题。我正在开发一个带有java和glassfish服务器的Web应用程序,用户可以在这个应用程序中进行注册,然后他们会发送一封带有激活链接的电子邮件。java发送带有激活链接的电子邮件

是否可以使用java邮件API发送邮件而无需外部smtp服务器?

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

我不想使用外部服务,如谷歌或雅虎。如果这是不可能的,你可以建议我一个开放源代码邮件服务器与玻璃鱼一起吗?我的意思是,是否有可能使用glassfish作为电子邮件服务器?如果不是,我还能使用什么?

谢谢!

回答

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 = "[email protected]"; 
     String from = "[email protected]"; 

     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(); 
     } 
    } 
} 
+0

感谢您的快速回答!不,我没有使用maven,是否有必要?我运行了你的代码,它给了我一个“** ConnectException:**拒绝连接” – BenSkeleton

+0

我再次搜索了一遍,发现我可以使用mac内置的postfix邮件程序,它可以从终端内启动,只需通过“sudo postfix start ”。它现在工作,非常感谢! – BenSkeleton

+0

@BenSkeleton感谢您的信息。 (Windows用户在这里;)) – arcs

0

是你可以做。

只需调用此函数即可向客户端发送自动电子邮件。 参数“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 = "[email protected]"; 
    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("[email protected]")); 
     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); 
    } 
} 
相关问题