Monday, June 30, 2014

Hi Everyone




Hi Surfman,



Here is the code it is working fine with normal internet but tried with network proxy no success and I'm getting exception like javax.mail.MessagingException: Could not connect to SMTP host, java.net.ConnectException: Connection refused. Is there any other way to write code for network proxy to send email as localhost please help me Thanx.




public class mail {



private static void main (string[] args) {




String[] to = {"***", "****"};




String host = "smtp.gmail.com";



String from = "****";



String pass = "****";




Properties properties = System.getProperties();




properties.put("mail.smtp.starttls.enable", "true");



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



properties.put("mail.smtp.user", from);



properties.put("mail.smtp.password", pass);



properties.put("mail.smtp.port", "587");//587,465,25



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



// properties.put("mail.smtp.debug", "true");




Session session = Session.getDefaultInstance(properties, null);



session.setDebug(true);




try {




MimeMessage message = new MimeMessage(session);




message.setFrom(new InternetAddress(from));




InternetAddress[] addressTo = new InternetAddress[to.length];



for (int i = 0; i < to.length; i++) {



addressTo[i] = new InternetAddress(to[i]);



}




message.setRecipients(Message.RecipientType.TO, addressTo);




message.setSubject("Hello");




BodyPart messageBodyPart = new MimeBodyPart();




messageBodyPart.setText("Hi All, \n\n"



+ "Please see the link bellow"



+ "\n\nKind regards,"



+ "\n Sandy");




Multipart multipart = new MimeMultipart();




multipart.addBodyPart(messageBodyPart);




messageBodyPart = new MimeBodyPart();



String filename = prop.getProperty("path") + FILENAME;



DataSource source = new FileDataSource(filename);



messageBodyPart.setDataHandler(new DataHandler(source));



messageBodyPart.setFileName(filename);



multipart.addBodyPart(messageBodyPart);




message.setContent(multipart);



// Send message



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



transport.connect(host, from, pass);



transport.sendMessage(message, message.getAllRecipients());



transport.close();



logger.info("Sent message successfully....");




} catch (MessagingException e) {



logger.error(e);



}



}







No comments:

Post a Comment