java邮件第四篇--使用java程序发送简单的文本邮件

一、前言

   经过前面几篇,邮件的发送原理已经清楚了,下面就是用java程序实现简单邮件的快速发送,其优点效率不但提高,在登录的时候也不需要使用base64的编码,直接输入字符串即可;

   代码的实现,与使用命令行的原理一样,又因为java已经实现对邮件模块的封装,是程序员的邮件开发更加简单,我们只需要调用相关的实现类即可实现邮件的发送;


二、详细的代码实现


import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
    public static void main(String args[]){
                                                   
            Properties properties = new Properties(); 
            properties.setProperty("mail.debug", "true");
            properties.setProperty("mail.transport.protocol", "smtp");
            Session session=Session.getDefaultInstance(properties);
        try {
                                                       
            //1、创建需要发送的邮件对象Message
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            //message.setReplyTo();
            message.setSubject("主题一:独垂泪");
            message.setText("fuck……");
                                                       
            //2、创建发送邮件的载体Transport
                                                       
            Transport transport=session.getTransport();
            transport.connect("smtp.sina.com", 25,"junxiuxiao", "xxxxxx");//服务器  端口   用户名  密码
                                                       
            //3、发送邮件
            transport.sendMessage(message, new Address[]{new InternetAddress("[email protected]")});
            transport.close();
        } catch (MessagingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } 
    }
}

三、由于使用debug模式,所以控制台的会有相关的输出,输出内容便是与服务器交互的过程,详细如下:

DEBUG: JavaMail version 1.4.3
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.sina.com", port 25, isSSL false
220 smtp2911-211.sinamail.sina.com.cn ESMTP
DEBUG SMTP: connected to host "smtp.sina.com", port: 25
EHLO xzb-PC
250-smtp2911-211.sinamail.sina.com.cn
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
AUTH LOGIN
334 VXNlcm5hbWU6
anVueGl1eGlhbw==
334 UGFzc3dvcmQ6
eGlubGFuZw==
235 OK Authenticated
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 ok
RCPT TO:<[email protected]>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 End data with <CR><LF>.<CR><LF>
From: [email protected]
Message-ID: <27994366.0.1369554617429.JavaMail.Administrator@xzb-PC>
Subject: =?UTF-8?B?5Li76aKY5LiA77ya54us5Z6C5rOq?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
ZnVja+KApuKApg==
.
250 ok queue id 5093688668121
QUIT
221 smtp2911-211.sinamail.sina.com.cn

四、登录邮箱查看邮件内容:

160536907.jpg



你可能感兴趣的:(ava)