2009-12-22 74 views
3

我需要使用JavaMail发送简单的html消息。当我试图在互联网上找到一些很好的例子和解释,每一个下一个例子都让我更加生气和愤怒。如何配置环境以使用JavaMail?

所有这些愚蠢的例子包含复制和粘贴它们的差别仅在评论和一个不错的声明,首先,你应该配置你的SMTP和POP3服务器的Java代码。

据我所知,没有人愿意让一个做广告的一些具体产品,但配置的服务器是恕我直言最难的部分。那么,任何人都可以给我一些关于配置具体服务器(例如Kerio或其他任何一个)的非常有用的信息(无需Java代码)?

我现在有什么是下一个异常:

250 2.0.0 Reset state 
javax.mail.SendFailedException: Invalid Addresses; 
    nested exception is: 
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Relaying to <[email protected]> denied (authentication required) 

UPD。以前所有文本的简单重写是:想象你有Windows,jdk,而没有别的。你想制作java程序并在你的机器上运行它。这个程序应该发送“Hello world!”到您的Gmail帐户。列出你的步骤。

UPD2。下面是代码:

Properties props = new Properties(); 
props.setProperty ("mail.transport.protocol", "smtp"); 
props.setProperty ("mail.host", "smtp.gmail.com"); 
props.setProperty ("mail.user", "[email protected]"); 
props.setProperty ("mail.password", "password_from_email_above"); 

Session mailSession = Session.getDefaultInstance (props, null); 
mailSession.setDebug (true); 
Transport transport = mailSession.getTransport(); 

MimeMessage message = new MimeMessage (mailSession); 
message.setSubject ("HTML mail with images"); 
message.setFrom (new InternetAddress ("[email protected]")); 
message.setContent ("<h1>Hello world</h1>", "text/html"); 
message.addRecipient (Message.RecipientType.TO, 
     new InternetAddress ("[email protected]")); 

transport.connect(); 
transport.sendMessage (message, 
     message.getRecipients (Message.RecipientType.TO)); 

和异常是:

RSET 
250 2.1.5 Flushed 3sm23455365fge.10 
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 3sm23455365fge.10 
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829) 
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886) 
    at com.teamdev.imgmail.MailSender.main(MailSender.java:33) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    ... 
+0

你的问题太含糊回答,因为它代表。请告诉我们更多关于您的主机及其上运行的组件的信息。继续品牌,我们不在乎。告诉我们你想做什么,什么是工作,什么不是。 – 2009-12-22 12:23:53

+0

@Carl Smotricz:我加了UPD部分。 – Roman 2009-12-22 12:29:48

+0

更新是一个*更*更好的问题。为此,事实证明,您甚至不需要自己的SMTP服务器,因为Google恰好为您运行一个相当大的服务器。 – 2009-12-22 12:32:50

回答

13

如果你正在寻找一个教程配置SMTP服务器,你不应该找的JavaMail。只要寻找您所选择的服务器上的教程(Kerio,例如...或EximSendMailApache JamesPostfix)或问上Serverfault。任何符合SMTP的服务器都可以很好地与JavaMail一起使用。

或者,你甚至可以使用任何“标准”的邮件提供商的基础设施。例如,我使用一个Google Apps帐户以及Google的SMTP基础结构从我们的Java应用程序发送邮件。 Using a Gmail account无论如何,如果您不想设置自己的SMTP服务器以简单地测试驱动器JavaMail,这是一个很好的起点。

作为最后的选择,你甚至可以查找的MX Records的域,并直接提供您的邮件到收件人的SMTP服务器。有一些常见的问题需要解决。

作为最后一点,你就必须考虑如何避免您的邮件被过滤为垃圾邮件 - 这是一个很大的话题本身。这里有助于依靠标准提供商来处理您在托管自己的服务器时可能遇到的一些问题。

btw:关于您发布的错误消息:SMTP服务器拒绝中继消息。这是因为如果您的SMTP服务器(认为它)正在example.com上运行,并且您正在将[email protected]发送到[email protected],那么您就要求SMTP服务器充当中继。这是几年前的惯例,直到它被垃圾邮件发送者滥用为止。从那时起,鼓励校长拒绝中继。您有两种选择:在发送邮件之前进行身份验证或发送到仅在您的服务器上托管的帐户(即在example.com上,例如[email protected])。

编辑:

下面是一些代码,以帮助您开始使用authenticationg(工作与Gmail帐户,但对于自己的服务器应该做的一样好)

private Session createSmtpSession() { 
    final Properties props = new Properties(); 
    props.setProperty("mail.smtp.host", "smtp.gmail.com"); 
    props.setProperty("mail.smtp.auth", "true"); 
    props.setProperty("mail.smtp.port", "" + 587); 
    props.setProperty("mail.smtp.starttls.enable", "true"); 
    // props.setProperty("mail.debug", "true"); 

    return Session.getDefaultInstance(props, new javax.mail.Authenticator() { 

    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication("[email protected]", "mypassword"); 
    } 
    }); 
} 
+0

谢谢。它的工作原理 – 2013-02-11 04:11:58

1

我可以看到你的问题的一部分。它在错误信息中有充分的解释。

您要发送邮件到的SMTP服务器(即您在JavaMail配置中配置的地址之一)拒绝将邮件转发到[email protected]。看起来像您的SMTP服务器中的配置问题。正如sfussenegger所指出的那样,它与javamail无关。

因此,您并未在所有方面同时进行调试,因此尝试使用已知的工作SMTP客户端来寻址您的SMTP服务器可能是个好主意。例如,雷鸟会很好。如果您可以通过它从Thunderbird发送邮件,那么JavaMail应该没什么问题。


更新:

的正确地址谷歌的SMTP服务器为:smtp.gmail.com。这是您在JavaMail中配置的服务器吗?你能向我们展示匹配的错误信息吗?

0

这应该工作:

import java.text.MessageFormat; 
import java.util.List; 
import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class Emailer { 

    public static void main(String[] args) { 

     String hostname = args[0]; 
     final String userName = args[1]; 
     final String passWord = args[2]; 
     String toEmail = args[3]; 
     String fromEmail = args[4]; 
     String subject = args[5]; 
     String body = ""; 
     // add rest of args as one body text for convenience 
     for (int i = 6; i < args.length; i++) { 
      body += args[i] + " "; 
     } 

     Properties props = System.getProperties(); 
     props.put("mail.smtp.host", hostname); 

     Session session = Session.getInstance(props, new Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(userName, passWord); 
      } 
     }); 

     MimeMessage message = new MimeMessage(session); 
     try { 
      message.setFrom(new InternetAddress(fromEmail)); 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); 
      message.setSubject(subject); 
      message.setText(body); 
      Transport.send(message); 

     } catch (MessagingException e) { 
      System.out.println("Cannot send email " + e); 
     } 
    } 
} 

你需要把了JavaMail的mail.jar在classpath的javax.mail依赖性。 我不确定Google是否允许您发送您想要的电子邮件。如何尝试其他电子邮件提供商,如您的ISP?

+0

只要我指定了正确的返回地址(或者我向Google注册的地址之一),Google就可以让我随时随地发送邮件(包括通过API)。 – 2009-12-23 12:39:36

2

工作示例结合上述答案,使用活化-1.1.jar邮件1.4.1.jar和SMTP主机是的Gmail

  1. 更换[email protected]user_pw符合return new PasswordAuthentication("[email protected]", "user_pw");

  2. 而且,你想,你想收到的电子邮件的邮件地址来替换[email protected]

    package com.test.sendEmail; 
    import java.util.Properties; 
    import javax.mail.*; 
    import javax.mail.internet.*; 
    
    public class sendEmailTest { 
    
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        sendEmailTest emailer = new sendEmailTest(); 
        //the domains of these email addresses should be valid, 
        //or the example will fail: 
        emailer.sendEmail(); 
    } 
    
    /** 
        * Send a single email. 
        */ 
    public void sendEmail(){ 
    Session mailSession = createSmtpSession(); 
    mailSession.setDebug (true); 
    
    try { 
        Transport transport = mailSession.getTransport(); 
    
        MimeMessage message = new MimeMessage (mailSession); 
    
        message.setSubject ("HTML mail with images"); 
        message.setFrom (new InternetAddress ("[email protected]")); 
        message.setContent ("<h1>Hello world</h1>", "text/html"); 
        message.addRecipient (Message.RecipientType.TO, new InternetAddress ("[email protected]")); 
    
        transport.connect(); 
        transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO)); 
    } 
    catch (MessagingException e) { 
        System.err.println("Cannot Send email"); 
        e.printStackTrace(); 
    } 
    } 
    
    private Session createSmtpSession() { 
    final Properties props = new Properties(); 
    props.setProperty ("mail.host", "smtp.gmail.com"); 
    props.setProperty("mail.smtp.auth", "true"); 
    props.setProperty("mail.smtp.port", "" + 587); 
    props.setProperty("mail.smtp.starttls.enable", "true"); 
    props.setProperty ("mail.transport.protocol", "smtp"); 
    // props.setProperty("mail.debug", "true"); 
    
    return Session.getDefaultInstance(props, new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("[email protected]", "user_pw"); 
        } 
    }); 
    } 
    
    } 
    
相关问题