无法使用javamail api将邮件发送到Gmail账户 [英] Not able to send mails to gmail account using javamail api

查看:127
本文介绍了无法使用javamail api将邮件发送到Gmail账户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public class MultiMimes {

public static void main(String [] args)抛出异常{

属性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);
尝试{
message.setSubject(我是多部分文本/ html电子邮件);
Address toAddress = new InternetAddress(my gmail address);
地址fromAddress =新InternetAddress(我的组织地址);
message.setFrom(fromAddress);
message.addRecipient(Message.RecipientType.TO,toAddress);

MimeMultipart multipart1 = new MimeMultipart(alternative);

//创建短信部分
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());
//创建html部分
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent( < HTML><主体>< b取代;是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());
//将多部分添加到消息
multipart1.addBodyPart(htmlPart);
multipart1.addBodyPart(textPart);
message.setContent(multipart1);

//发送消息
Transport.send(message);
System.out.println(邮件发送成功);
catch(AddressException ae){
System.out.println(address exception);
ae.printStackTrace();
}
catch(MessagingException e){
System.out.println(message exception);
e.printStackTrace();
}


}

当我使用来自同一个域名的电子邮件地址(例如:somebody@test.com)而不是gmail id,我收到了这封电子邮件。

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



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



任何方式...我发现解决方案我的问题与Prophesy等待的建议......我修改了我的代码如下:

  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;
导入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 {

属性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);
尝试{
message.setSubject(我是多部分文本/ html电子邮件);
Address toAddress = new InternetAddress(my gmail address);
地址fromAddress =新InternetAddress(我的组织地址);
message.setFrom(fromAddress);
message.addRecipient(Message.RecipientType.TO,toAddress);
MimeMultipart multipart1 = new MimeMultipart(alternative);

//创建短信部分
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());
//创建html部分
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent( < HTML><主体>< b取代;是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());
//将多部分添加到消息

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

//发送消息
Transport.send(message);
System.out.println(邮件发送成功);
catch(AddressException ae){
System.out.println(address exception);
ae.printStackTrace();
}
catch(MessagingException e){
System.out.println(message exception);
e.printStackTrace();
}


}

}

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


When I am trying to send a sample mail to gmail account, am getting com.sun.mail.smtp.SMTPAddressFailedException. Following is the code i have written... Please can somebody help me to resolve the issue?

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


}

When i am using an email id from same domain (for ex: somebody@test.com) instead of gmail id, I am receiving the email.

解决方案

Thanks to all...As i am new to satckoverflow... I am not able to find exact way to add appropriate comments....

Here I am not trying to send mails via gmail server... I am just trying to send to some gmail user. with this code i am able to send mails to any one within the organization... but not to people outside the organization.

any ways... I found the solution to my problem with "Prophesy Awaits" suggestion... I modified my code as below:

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


        }

   }

and i used same SMTPAuthenticator class provided by him... now i am able to send the mails form my organizations mailserver to any email id... Thanks again.

这篇关于无法使用javamail api将邮件发送到Gmail账户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆