2010-03
31

Java Jmail中配置Gmail配置

Write by Airene

程序代码如下,搞了好久:

import java.security.Security;
import java.util.Date;
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;

import com.zhyone.base.Util;
public class Jmail extends BaseClass {
	public void sendMail() throws MessagingException, Exception {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", "smtp.gmail.com");
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.auth", "true");
		final String username = "****";
		final String password = "****";
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(username + "@gmail.com"));
		msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("****@qq.com", false));
		msg.setSubject("A - " + Util.toUtf8(request.getParameter("title")));
		msg.setText(  Util.toUtf8(request.getParameter("uname"))+" - "+request.getParameter("addr") + " - " + Util.toUtf8(request.getParameter("content")));
		msg.setSentDate(new Date());
		Transport.send(msg);
		context.put("msg", Util.sendMsg("success!", "/index.jsp"));
		tempatePath = TemplateKey.AlertMsg;
	}
}
Tags : java   jmail   gmail  
Class : Java

 

  • 名字: *
  • 邮箱:
  • 网址:
  • 验码:
  • 内容:
   

i Categorys

CopyRight © 2008 - 2011 Airene.NET
Powered by ZhyoneBlog4j-1.0 京ICP备09069893号