Total members 11890 |It is currently Tue Apr 23, 2024 1:23 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Hi i want to send email using java program.
CAn you help me?




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

use Java Mail API. But you also need a SMTP address.

Code:
public class Mail {
String d_email = "from@...",
d_password = "***",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "to@...",
m_subject = "subject",
m_text = "this is just a test mail";

public Mail() {
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "
javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
//session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(m_to));
Transport.send(msg);
} catch (Exception mex) {
mex.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}

public static void main(String[] args){
Mail sendmail=new Mail();
}
}


_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time

cool

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : email through JAVA program
 Java Email Class     -  
 How to create Email from Core Java Code     -  
 java program     -  
 java program     -  
 java chat program.     -  
 Executing a Java Program     -  
 Elements of a Java Program     -  
 compiling and getting of a java program     -  
 Java Chat Program between two computers     -  
 Java Program for communicate PC and Mobile     -  



Topic Tags

Java Email
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com