java发送文本邮件,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("junxiuxiao@sina.com"));

//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("junxiuxiao@163.com")});

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:

250 ok

RCPT TO:

250 ok

DEBUG SMTP: Verified Addresses

DEBUG SMTP: junxiuxiao@163.com

DATA

354 End data with .

From: junxiuxiao@sina.com

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

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

a414950fb49a9a974b6ad8ed5d5b32e8.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值