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.
First of all , you need to change your code part to this :
And here is the SMTPAuthenticator Class :
}
And please do change your from address to gmail and to address to anything, since your SMTP Authentication is being done by gmail, so that part must belong to gmail.
Hope that might help.
Regards
i think the host name is a problem so change mailservername to smtp.gmail.com
for more information use this link
http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
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:
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.