cancel
Showing results for 
Search instead for 
Did you mean: 

JAVA Mail :Could not connect to SMTP Host

Former Member
0 Kudos

Hi folks,

I am triggering an email using web dynpro UI where my hostName is serverXX.com and portNo XXX .

When i tried using default port no, then gives me error like

Could not connect to SMTP host: serverXX, port: 25;

Then i used

Properties property = new Properties();

property.put("mail.smtp.port","XXX");

This does not gives any error but processing enter into an infinite loop that never ends.Then i have to close the window. If above port is other than our used port then again it gives above told error (Port 25).

Thanks in anticipation,

Mandeep Virk

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

lease use below mentioned code:

String strMailDetail[] = new String10;

StringBuffer strMailBody;

try

{

strMailBody = new StringBuffer();

Properties props;

Session session;

Provider p;

String strSMTPIPAddress = // Enter SMTP Server address here

java.util.Date todayDate = new java.util.Date();

// Get System Properties

props = System.getProperties();

// Setup SMTP Mail Server

props.put("mail.smtp.host", strSMTPIPAddress);

// Get session without Authenticator

session = Session.getDefaultInstance(props, null);

p = session.getProvider("smtp");

Message message = new MimeMessage(session);

message.setFrom("email_ID");

message.addRecipient(Message.RecipientType.TO, "email_ID");

message.addRecipient(Message.RecipientType.CC, "email_ID");

MimeBodyPart mbp = new MimeBodyPart();

mbp.setText(strMailBody.toString());

Multipart mt = new MimeMultipart();

message.setContent(mt);

message.setSentDate(new java.util.Date());

/*****************************/

//Send message

Transport.send(message);

/*****************************/

}

catch (Exception e)

{

wdComponentAPI.getMessageManager().reportException(e+" in Send Mail method", false);

}

Former Member
0 Kudos

hi,

i got the same problem then i did this hope it will work for u also.

i did it for gmail.

Properties props = new Properties();

String host = "smtp.gmail.com";

String port = "465";

props.put("mail.smtp.host", host);

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.port", port);

props.put("mail.smtp.socketFactory.port", port);

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.socketFactory.fallback", "false");

Regards

Trilochan

Former Member
0 Kudos

Hi Guys,

thanks for your response.

It was good piece of code, after deploying this code when i triggered mail it give me following message exception:

Sending failed; nested exception is: javax.mail.AuthenticationFailedException

can u please help me out with code for authenticator.

Mandeep Virk

Former Member
0 Kudos

hi,

for authentication use this code

Session session = Session.getInstance(props, new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(username, password);

}

});

Regards

Trilochan

Former Member
0 Kudos

hi trilochan,

Thanks

Now i am able to send email properly.

In my syntax i used a code snippet to validate user input for mail recipient's.

if(email instanceof String){
		 String[] result = email.split("@");
		 if(result.length == 2){
			return true;	
		 }	}

Now i want to enter multiple recipient's for mail but this time it gives error used for above validation "Invalid email"(user defined).

For multiple recipient's i wrote a code

message.setRecipients( javax.mail.Message.RecipientType.TO, InternetAddress.parse( 
	               wdContext.currentEmailDataElement().getToAddress(), false));

Any Idea

Mandeep Virk

Former Member
0 Kudos

Hi.

There is a complete list of properties on this page:

http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html

You need to authenticate explicitly with Transport.connect.

Try this example listed on the same page:

Transport tr = session.getTransport("smtp");

tr.connect(smtphost, username, password);

msg.saveChanges(); // don't forget this

tr.sendMessage(msg, msg.getAllRecipients());

tr.close();

Regards, Mikael