I am getting the below error while sending email from corporate outlook using java mail API.
javax.mail.MessagingException: Could not connect to SMTP host: smtp.mycompany.net.au, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
I am able to telnet the server with the same port my machine.What could be the root cause of the issue?
Code used for sending email is -
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", mysmtpserver);
props.put("mail.smtp.port", myport);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmailAddress));
message.setRecipients(Message.RecipientType.BCC, addressTo);
message.setSubject(emailSubject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setHeader("Content-Type", "text/html;charset=UTF-8");
messageBodyPart.setHeader("Content-Transfer-Encoding", "quoted-printable");
messageBodyPart.setContent(emailBody, "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
//part 1-add html part
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
String filename = reportFilePath.trim();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
Appreciated any help in resolving it.
Thanks Libin
java.net.ConnectException: Connection refused: connect error throws due to the following reasons that you have to check:
Thanks for providing your code. Pleas apply the following code. Its very easy understandable and works fine. 100% tested
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.event.*;
import java.net.*;
import javax.activation.*;
public class SendEmail extends ConfigProperties{
public static void send(String from, String to, String subject, String content) throws AddressException, MessagingException{
ErrorCheck ec = new ErrorCheck();
String error = "";
try{
error = ec.error();
String smtp = getPropVal("SMTP");
String host = getPropVal("HOST");
Properties props=new Properties();
props.put(smtp,host);
Session session1 = Session.getDefaultInstance(props,null);
Message msg =new MimeMessage(session1);
msg.setHeader("Content-Type", "text/plain; charset=UTF-8");
msg.setHeader("Content-Transfer-Encoding", "quoted-printable");
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setContent(content,"text/html; charset=UTF-8");
Transport.send(msg);
}catch(Exception e){
ec.errorMsg(error+"SendEmail.send()", e);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.