Gửi mail có chứng thực trong java

Với gói javamail của sun, việc gửi mail của bạn trở nên cực kỳ dễ dàng. Sau đây là 1 chương trình gửi mail hoàn chỉnh với tài khoản gmail và được validate ngon lành. Bạn copy đoạn code sau đó dùng notepad tạo file SendMailOK.java, paste đoạn code vào, lưu lại

Bạn phải download gói java mail của sun, cài đặt và thiết lập classpath đến file mail.jar trước khi thực thi ứng dụng.
http://java.sun.com/products/javamail/downloads/index.html
Ngoài ra, bạn còn phải thiết lập classpath đến 2 gói activation.jar và javaee.jar (2 gói này nằm trong glassfish\lib folder nếu dùng Glassfish)

Để thực thi, chạy command line, gõ java SendMailOK.java để biên dịch, sau đó gõ javac SendMailOK để chạy.

package gmail;

import java.util.Date;
import java.util.Properties;

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

public class SendMailOK {

public static void send(String smtpServer, String to, String from,String psw,
String subject, String body) throws Exception{
// java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = System.getProperties();
// –
props.put(”mail.smtp.host”, smtpServer);
props.put(”mail.smtp.port”, “587″);
props.put(”mail.smtp.starttls.enable”,”true”);
final String login = from;//”[email protected]”;//usermail
final String pwd = psw;//”password cua ban o day”;
Authenticator pa = null; //default: no authentication
if (login != null && pwd != null) { //authentication required?
props.put(”mail.smtp.auth”, “true”);
pa = new Authenticator (){
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(login, pwd);
}
};
}//else: no authentication
Session session = Session.getInstance(props, pa);
// — Create a new message –
Message msg = new MimeMessage(session);
// — Set the FROM and TO fields –
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));

// — Set the subject and body text –
msg.setSubject(subject);
msg.setText(body);
// — Set some other header information –
msg.setHeader(”X-Mailer”, “LOTONtechEmail”);
msg.setSentDate(new Date());
msg.saveChanges();
// — Send the message –
Transport.send(msg);
System.out.println(”Message sent OK.”);

}
/**
* Main method to send a message given on the command line.
*/
public static void main(String[] args) {
{
try
{
String smtpServer=”smtp.gmail.com”;
String to=”[email protected]”;
String from=”[email protected]”;
String subject=”Hello from Java”;
String body=”Test using java to send mail.”;
String password=”mật khẩu của bạn ở Ä‘Ă¢y”;
send(smtpServer, to, from, password, subject, body);
System.out.println(”Finish!”);
}
catch (Exception ex)
{
System.out.println(”Usage: “+ex.getMessage());
}

}

/**
* “send” method to send the message.
*/

}

}

Advertisement

Share this:

Like this:

Like

Loading…