Javamail smtp port number

A

autogoor

I use the following code to send email. I do not know to use the
default 25 for smtp port. How can I set a different number?

public static void sendMail(
String smtpServer,
String to,
String cc,
String from,
String subject,
String body){


try{


Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);


Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,


InternetAddress.parse(to, false));


if (cc != null){
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
}


msg.setSubject(subject);
msg.setText(body);
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
Transport.send(msg);


System.out.println("Mail sent OK");
}catch (Exception ex){
ex.printStackTrace();
}
}
 
R

Roland

I use the following code to send email. I do not know to use the
default 25 for smtp port. How can I set a different number?

public static void sendMail(
String smtpServer,
String to,
String cc,
String from,
String subject,
String body){


try{


Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", Integer.toString(888));
Session session = Session.getDefaultInstance(props, null);


Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,


InternetAddress.parse(to, false));


if (cc != null){
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
}


msg.setSubject(subject);
msg.setText(body);
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
Transport.send(msg);


System.out.println("Mail sent OK");
}catch (Exception ex){
ex.printStackTrace();
}
}
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
A

autogoor

Thank you so much. Now the port part worked. But I still got the
following message. I think I need to gave username and password for my
1and1 account. Could you tell me how to set username and password?

THanks, again
ggor

javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 550 won't deliver
to <[email protected]>

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1130)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:525)
at javax.mail.Transport.send0(Transport.java:151)
at javax.mail.Transport.send(Transport.java:80)
 
A

Anthony Borla

Thank you so much. Now the port part worked. But I still
got the following message. I think I need to gave username
and password for my 1and1 account. Could you tell me how
to set username and password?

First, ensure that you do this:

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

Then, change your approach slightly - instead of this:

Transport.send(msg);

do something like:

...
msg.saveChanges();
...
// connect to the transport
Transport transport = session.getTransport("smtp");
transport.connect(smtpHost, " ", " "); // host, user, password

// send the msg and close the connection
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
...

Once again, check the FAQ - I previously posted the link.

I hope this helps.

Anthony Borla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top