[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java mail

Posted on 2010-01-05
7
Medium Priority
?
499 Views
Last Modified: 2013-12-15
code for a simple to programe to send a mail using javamail api
0
Comment
Question by:mkarthik415
7 Comments
 
LVL 22

Accepted Solution

by:
kadaba earned 668 total points
ID: 26183268
0
 
LVL 5

Assisted Solution

by:afibarra
afibarra earned 668 total points
ID: 26183311
Hope this helps...

package mx.com.biovetsa.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SimpleSmtp {

	private String host;
	private String port;
	private String from;
	private String to;
	private String username;
	private String password;
	private String subject;
	private String msg;
	private boolean authSmtp;
	Properties prop;
	Session session;
	MimeMessage mimeMsg;
	Store store;
	
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	
	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}
	
	public boolean isAuthSmtp() {
		return authSmtp;
	}
	public void setAuthSmtp(boolean authSmtp) {
		this.authSmtp = authSmtp;
	}
	
	public void sendMessage() {
		
		try {
			this.prop = new Properties();
			this.prop.put("mail.smtp.host", this.getHost());
			this.prop.put("mail.smtp.port", this.getPort());

			if(this.authSmtp) {
				this.prop.put("mail.smtp.auth", "true");
				Authenticator auth = new PopupAuthenticator(this.getUsername(), this.getPassword());
				this.session = Session.getInstance(this.prop, auth);
			} else {
				this.session = Session.getDefaultInstance(this.prop, null);
			}
			
			this.mimeMsg = new MimeMessage(this.session);
			this.mimeMsg.setFrom(new InternetAddress(this.getFrom()));
			this.mimeMsg.addRecipient(Message.RecipientType.TO, new InternetAddress(this.getTo()));
			this.mimeMsg.setSubject(this.getSubject());
			this.mimeMsg.setText(this.getMsg());
			Transport.send(this.mimeMsg);
			
		}catch(NoSuchProviderException nspe) {
			nspe.printStackTrace(System.err);
		}catch(MessagingException me) {
			me.printStackTrace(System.err);
		}
	}
}

class PopupAuthenticator extends Authenticator {

	String username;
	String password;

	public PopupAuthenticator(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(this.username, this.password);
	}
}

Open in new window

0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 664 total points
ID: 26207277
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:mkarthik415
ID: 26342493
Hi,
Thank you very much for your replies. I am using this code to send mail through java, but I have not received any mails. I have got no exceptions also.
TY


import java.util.Properties;

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;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailWithPasswordAuthentication {
	public static void main(String[] args) throws MessagingException {
		new MailWithPasswordAuthentication().run();
	}

	private void run() throws MessagingException {
		Message message = new MimeMessage(getSession());

		message.addRecipient(RecipientType.TO, new InternetAddress("mkarthik415@gmail.com"));
		message.addFrom(new InternetAddress[] { new InternetAddress("mkarthik415@gmail.com") });

		message.setSubject("the subject");
		message.setContent("the body", "text/plain");
		System.out.println("message sent");

		Transport.send(message);
	}

	private Session getSession() {
		Authenticator authenticator = new Authenticator();

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
		properties.setProperty("mail.smtp.auth", "true");

		properties.setProperty("mail.smtp.host", "smtp.gmail.com");
		properties.setProperty("mail.smtp.port", "465");

		return Session.getInstance(properties, authenticator);
	}

	private class Authenticator extends javax.mail.Authenticator {
		private PasswordAuthentication authentication;

		public Authenticator() {
			String username = "mkarthik415";
			String password = "*******";
			System.out.println("selecting username and password");
			authentication = new PasswordAuthentication(username, password);
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return authentication;
		}
	}
}

Open in new window

0
 

Author Comment

by:mkarthik415
ID: 26343922
After running the project with above code as java applicatioin I getting this exception

selecting username and password
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1694)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
      at javax.mail.Service.connect(Service.java:291)
      at javax.mail.Service.connect(Service.java:172)
      at javax.mail.Service.connect(Service.java:121)
      at javax.mail.Transport.send0(Transport.java:190)
      at javax.mail.Transport.send(Transport.java:120)
      at MailWithPasswordAuthentication.run(MailWithPasswordAuthentication.java:26)
      at MailWithPasswordAuthentication.main(MailWithPasswordAuthentication.java:14)
0
 
LVL 15

Expert Comment

by:fsze88
ID: 26346019
0
 

Author Closing Comment

by:mkarthik415
ID: 31673087
ty
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question