java - Send an email to my work's email server with JavaMail -
i have searched through many other posts on here users similar problems can't seem work.
package testingstuff; import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.message.recipienttype; import javax.mail.internet.*; import javax.activation.*; public class test { public static void main(string[] args) { // todo auto-generated method stub system.out.println("hello world"); final string user = "my work id"; final string pw = "my work password"; properties props = new properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "server got our outlook app"); props.put("mail.smtp.port", "587"); session session = session.getinstance(props, new javax.mail.authenticator() { protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(user, pw); } }); session.setdebug(true); try { message msg = new mimemessage(session); string = "kevin.camacho@company.com"; string = "my.colleague@company.com"; msg.setfrom(new internetaddress(from)); msg.setrecipient(recipienttype.to, new internetaddress(to)); msg.setsubject("test"); msg.settext("testing"); //transport trans = session.gettransport("smtp"); //trans.connect("smtp-mail.outlook.com", 25, user, pw); transport.send(msg); } catch (exception e) { system.out.println(e); } system.out.println("done"); } }
i using java 1.8, javamail reports version 1.4.7
here output session debug:
hello world debug: setdebug: javamail version 1.4.7 debug: getprovider() returning javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,oracle] debug smtp: useehlo true, useauth true debug smtp: useehlo true, useauth true debug smtp: trying connect host "[my work email server]", port 587, isssl false javax.mail.messagingexception: not connect smtp host: [my work email server], port: 587;
nested exception is:
java.net.connectexception: connection timed out: connect
done
the email server got using out outlook app. went account settings, selected account , selected change. opens window config email , contains server address.
try below code, put in username,password,to,from,host
import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class sendemail { public static void main(string [] args) { string username = ""; string password = ""; string = ""; string = ""; string host = ""; properties properties = system.getproperties(); properties.setproperty("mail.smtp.host", host); properties.setproperty("mail.smtp.user", username); properties.setproperty("mail.smtp.password", password); properties.setproperty("mail.smtps.auth", "true"); session session = session.getdefaultinstance(properties); try{ mimemessage message = new mimemessage(session); message.setfrom(new internetaddress(from)); message.addrecipient(message.recipienttype.to, new internetaddress(to)); message.setsubject("this subject line!"); bodypart messagebodypart = new mimebodypart(); messagebodypart.settext("this message body"); multipart multipart = new mimemultipart(); multipart.addbodypart(messagebodypart); message.setcontent(multipart); message.savechanges(); transport transport = session.gettransport("smtp"); transport.connect(host, username, password); transport.sendmessage(message, message.getallrecipients()); transport.close(); system.out.println("sent message successfully...."); }catch (messagingexception mex) { mex.printstacktrace(); } } }
Comments
Post a Comment