java邮件发送

1.sendEmail方式

</pre><pre name="code" class="java"><pre name="code" class="java">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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class sendEmail
{
	public static void main(String[] args) throws AddressException,
			MessagingException
	{
		String SEND_USER = "135xxxx@163.com";
		String SEND_UNAME = "135xxxx";
		String SEND_PWD = "abcabc";//密码
		String VALUE_SMTP = "smtp.163.com";
		// Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		// final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		// Get a Properties object
		Properties props = System.getProperties();
		// props.setProperty("mail.smtp.host", "smtp.gmail.com");
		props.setProperty("mail.smtp.host", "smtp.163.com");
		// props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		// props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", "25");
		// props.setProperty("mail.smtp.port", "587");
		// props.setProperty("mail.smtp.socketFactory.port", "25");
		// props.setProperty("mail.smtp.socketFactory.port", "587");
		props.put("mail.smtp.auth", "true");
		final String username = "135xxxx";
		final String password = "abcabc";
		Session session = Session.getDefaultInstance(props,
				new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication()
					{
						return new PasswordAuthentication(username, password);
					}
				});

		// -- Create a new message --
		session.setDebug(true);
		Message msg = new MimeMessage(session);

		// -- Set the FROM and TO fields --
		msg.setFrom(new InternetAddress(username + "@163.com"));
		msg.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse("135xxxx@163.com", false));
		msg.setSubject("Hello---");
		msg.setText("How are you");
		msg.setSentDate(new Date());
		Transport transport = session.getTransport("smtp");
		// smtp验证,就是你用来发邮件的邮箱用户名密码
		transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
		// 发送
		transport.sendMessage(msg, msg.getAllRecipients());
		Transport.send(msg);
		transport.close();

		System.out.println("Message sent.");
	}
}



2.SendmailUtil方式
</pre><pre name="code" class="java">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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendmailUtil
{

	// 设置服务器
    private static String KEY_SMTP = "mail.smtp.host";
    private static String VALUE_SMTP = "smtp.163.com";
    // 服务器验证
    private static String KEY_PROPS = "mail.smtp.auth";
    private static boolean VALUE_PROPS = true;
    // 发件人用户名、密码
    private String SEND_USER = "13xxxx@163.com";
    private String SEND_UNAME = "<span style="font-family: Arial, Helvetica, sans-serif;">13xxxx</span><span style="font-family: Arial, Helvetica, sans-serif;">";</span>
    private String SEND_PWD = "abcabc";
    // 建立会话
    private MimeMessage message;
    private Session s;
 
    /*
     * 初始化方法
     */
    public SendmailUtil() {
        Properties props = System.getProperties();
        props.setProperty(KEY_SMTP, VALUE_SMTP);
        props.put(KEY_PROPS, "true");
        //props.put("mail.smtp.auth", "true");
        s =  Session.getDefaultInstance(props, new Authenticator(){
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
              }});
        s.setDebug(true);
        message = new MimeMessage(s);
    }
 
    /**
     * 发送邮件
     * 
     * @param headName
     *            邮件头文件名
     * @param sendHtml
     *            邮件内容
     * @param receiveUser
     *            收件人地址
     */
    public void doSendHtmlEmail(String headName, String sendHtml,
            String receiveUser) {
        try {
            // 发件人
            InternetAddress from = new InternetAddress(SEND_USER);
            message.setFrom(from);
            // 收件人
            InternetAddress to = new InternetAddress(receiveUser);
            message.setRecipient(Message.RecipientType.TO, to);
            // 邮件标题
            message.setSubject(headName);
            String content = sendHtml.toString();
            // 邮件内容,也可以使纯文本"text/plain"
            message.setContent(content, "text/html;charset=GBK");
            message.saveChanges();
            Transport transport = s.getTransport("smtp");
            // smtp验证,就是你用来发邮件的邮箱用户名密码
            transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
            // 发送
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            System.out.println("send success!");
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        SendmailUtil se = new SendmailUtil();
        se.doSendHtmlEmail("邮件头文件名", "邮件内容", "186xxxx@163.com");
    }
}

ps: 163邮箱有安全限制,需要登陆邮箱之后在“设置”里开启并使用授权码代替邮箱的登陆密码。



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/abc58396683/article/details/52347478
文章标签: java 邮件 class 方法
个人分类: Java
上一篇java反射
下一篇回调函数 callback function
想对作者说点什么? 我来说一句

Java实现邮件发送

2017年12月19日 19.44MB 下载

JAVA实现邮件发送.txt

2011年04月22日 8KB 下载

java 邮件发送测试

2011年08月15日 480KB 下载

java邮件发送组件

2009年04月21日 239KB 下载

Java 邮件发送源码

2010年08月04日 501KB 下载

java邮件发送工具类

2009年11月25日 4KB 下载

没有更多推荐了,返回首页

关闭
关闭