0

当我尝试向 gmail 帐户发送示例邮件时,收到 com.sun.mail.smtp.SMTPAddressFailedException。以下是我编写的代码...请有人帮我解决这个问题吗?

    public class MultiMimes {

public static void main(String[] args) throws Exception{

    Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", "mailservername");
    props.put("mail.debug", "true");
    Session session = Session.getDefaultInstance(props,null);
    Message message = new MimeMessage(session);
    try{
        message.setSubject("I am a multipart text/html email" );
        Address toAddress =new InternetAddress("my gmail address");
        Address fromAddress =new InternetAddress("my organization address");
        message.setFrom(fromAddress);
        message.addRecipient(Message.RecipientType.TO, toAddress);

        MimeMultipart multipart1 = new MimeMultipart("alternative");

        // Create text message part
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent("am text", "text/plain");
        textPart.setHeader("MIME-Version" , "1.0" );
        textPart.setHeader("Content-Type" , textPart.getContentType() );
          System.out.println("textPart.getContentType():"+textPart.getContentType());
        // Create html part
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("<html><body><b>am html</b></body></html>", "text/html");
        htmlPart.setHeader("MIME-Version" , "1.0" );
        htmlPart.setHeader("Content-Type" , "text/html" );
        System.out.println("htmlPart.getContentType():"+htmlPart.getContentType());
        //adding multiparts to message
        multipart1.addBodyPart(htmlPart);
        multipart1.addBodyPart(textPart);
        message.setContent(multipart1);

        //sending message
        Transport.send(message);
        System.out.println("mail sent successfully");
    }catch(AddressException ae){
        System.out.println("address exception");
        ae.printStackTrace();
    }
    catch(MessagingException e){
        System.out.println("message exception");
        e.printStackTrace();
    }


}

当我使用来自同一域的电子邮件 ID(例如:someone@test.com)而不是 gmail id 时,我会收到电子邮件。

4

3 回答 3

1

感谢所有人......因为我是 satckoverflow 的新手......我无法找到添加适当评论的确切方法......

在这里,我不是想通过 gmail 服务器发送邮件......我只是想发送给一些 gmail 用户。使用此代码,我可以将邮件发送给组织内的任何人......但不能发送给组织外的人。

任何方式......我找到了“Prophesy Awaits”建议的解决方案......我修改了我的代码如下:

    package com.trx;

    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Address;
    import javax.mail.Authenticator;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;

    public class MultiMimes {

public static void main(String[] args) throws Exception{

    Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", "mymailserver");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    Authenticator auth = new SMTPAuthenticator();
    Session session = Session.getDefaultInstance(props, auth);
    Message message = new MimeMessage(session);
    try{
        message.setSubject("I am a multipart text/html email" );
        Address toAddress =new InternetAddress("my gmail address");
        Address fromAddress =new InternetAddress("my organization address");
        message.setFrom(fromAddress);
        message.addRecipient(Message.RecipientType.TO, toAddress);
        MimeMultipart multipart1 = new MimeMultipart("alternative");

        // Create text message part
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent("am text", "text/plain");
        textPart.setHeader("MIME-Version" , "1.0" );
        textPart.setHeader("Content-Type" , textPart.getContentType() );
          System.out.println("textPart.getContentType():"+textPart.getContentType());
        // Create html part
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("<html><body><b>am html</b></body></html>",  "text/html");
        htmlPart.setHeader("MIME-Version" , "1.0" );
        htmlPart.setHeader("Content-Type" , "text/html" );
        System.out.println("htmlPart.getContentType():"+htmlPart.getContentType());
        //adding multiparts to message

        multipart1.addBodyPart(textPart);
        multipart1.addBodyPart(htmlPart);
        message.setContent(multipart1);

        //sending message
        Transport.send(message);
        System.out.println("mail sent successfully");
    }catch(AddressException ae){
        System.out.println("address exception");
        ae.printStackTrace();
    }
    catch(MessagingException e){
        System.out.println("message exception");
        e.printStackTrace();
    }


        }

   }

我使用了他提供的相同的 SMTPAuthenticator 类...现在我可以将邮件从我的组织邮件服务器发送到任何电子邮件 ID...再次感谢。

于 2011-12-28T07:24:35.490 回答
1

首先,您需要将代码部分更改为:

private static final String SMTP_HOST_NAME = "smtp.gmail.com";

Properties prop = new Properties();
prop.put("mail.smtp.host", SMTP_HOST_NAME);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(prop, auth);

session.setDebug(debug);

这是 SMTPAuthenticator 类:

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
  * SimpleAuthenticator is used to do simple authentication
  * when the SMTP server requires it.
  */

public class SMTPAuthenticator extends Authenticator
{
  private static final String SMTP_AUTH_USER = "youremail@gmail.com";
  private static final String SMTP_AUTH_PASSWORD = "yourpassword";

  public PasswordAuthentication getPasswordAuthentication()
  {
    String username = SMTP_AUTH_USER;
    String password = SMTP_AUTH_PASSWORD;

   return new PasswordAuthentication(username,  password);
  }

}

请务必将您的地址更改为 gmail 并将地址更改为任何地址,因为您的 SMTP 身份验证是由 gmail 完成的,因此该部分必须属于 gmail。

希望这可能会有所帮助。

问候

于 2011-12-27T16:53:55.457 回答
0

我认为主机名有问题,所以将 mailservername 更改为 smtp.gmail.com

有关更多信息,请使用此链接

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

于 2011-12-27T16:47:01.653 回答