Sending an Email from Java using APIs, not with localhost -



Sending an Email from Java using APIs, not with localhost -

i'm using code , apis found here: http://www.tutorialspoint.com/java/java_sending_email.htm

when run code, error output thus:

com.sun.mail.util.mailconnectexception: couldn't connect host, port: localhost, 25; timeout -1; nested exception is: java.net.connectexception: connection refused @ com.sun.mail.smtp.smtptransport.openserver(smtptransport.java:2053) @ com.sun.mail.smtp.smtptransport.protocolconnect(smtptransport.java:697) @ javax.mail.service.connect(service.java:364) @ javax.mail.service.connect(service.java:245) @ javax.mail.service.connect(service.java:194) @ javax.mail.transport.send0(transport.java:253) @ javax.mail.transport.send(transport.java:124)

as plain see first line, primary issue "couldn't connect host, port: localhost" blah blah.

alright. so, have ideas should using instead of localhost? totally not area of expertise.

(the error log considerably longer, but, there's lot of code beingness bounced around. if whatever reason want whole thing, allow me know , i'll update it)

update:

i want give thanks stackoverflow community of posts i've seen around site on topic, , helped me reply issue. please find below finished code receive email object (from class) , send out! note, took out username , password gmail account, :)

import java.util.arraylist; import java.util.properties; import javax.mail.authenticator; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; public class sendemail { private class smtpauthenticator extends authenticator { public passwordauthentication getpasswordauthentication() { homecoming new passwordauthentication("username@gmail.com", "password"); } } public void createandsendemailmessage(arraylist<?> messagecontents) throws messagingexception { email email = new email(); email.setrecipient(messagecontents.get(0) + ""); email.setsender("username@gmail.com"); email.setsubject(messagecontents.get(1) + ""); email.setmessagecontent(messagecontents.get(2)+""); sendemailmessage(email); } public void sendemailmessage(email email) throws messagingexception { // scheme properties properties props = system.getproperties(); props = new properties(); props.put("mail.smtp.user", "username@gmail.com"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.debug", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketfactory.port", "587"); props.put("mail.smtp.socketfactory.class", "javax.net.ssl.sslsocketfactory"); props.put("mail.smtp.socketfactory.fallback", "false"); smtpauthenticator auth = new smtpauthenticator(); session session = session.getinstance(props, auth); session.setdebug(false); mimemessage msg = new mimemessage(session); msg.settext(email.getmessagecontent()); msg.setsubject(email.getsubject()); msg.setfrom(new internetaddress(email.getsender())); msg.addrecipient(message.recipienttype.to, new internetaddress(email.getrecipient())); transport transport = session.gettransport("smtps"); transport.connect("smtp.gmail.com", 465, "username", "password"); transport.sendmessage(msg, msg.getallrecipients()); transport.close(); } }

since find annoying when people post 1 part of code, not code calls it, i'm going show that, well!

arraylist<string> emailinfo = new arraylist<string>(); emailinfo.add(useremailaddress.gettext()+"@gmail.com"); emailinfo.add("an business relationship has been created you!"); emailinfo.add("here message"); sendemail newemail = new sendemail(); seek { newemail.createandsendemailmessage(emailinfo); } grab (messagingexception e1) { // todo auto-generated grab block e1.printstacktrace(); }

this trying utilize smtp server on local machine. if you're on linux box, install sendmail , configure that... otherwise need using email provider's smtp service.

this won't simple: you'll need authentication , you'll need ssl.

update: you're using gmail, should able gmail smtp sever , config needs. either smtp.gmail.com or mail.gmail.com. needs authentication , requires ssl.

you'll need alter localhost right smtp server address, starter. , authentication covered @ bottom of tutorial linked to. it'll want gmail username , password.

java email gmail localhost port

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -