java发送邮件

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPTransport;

public class sendMailTest
{

public static void main(String args[])
{
String protocol = "smtp"; //协议
String hostName = "smtp.sina.com.cn"; //SMTP服务器地址
Properties props = new Properties();
props.put("mail.transport.protocol", protocol);
props.put("mail.smtp.host", hostName);
props.put("mail.smtp.localhost", "localhost");
props.put("mail.store.protocol", "25");
props.put("mail.smtp.auth", "true");

Session mailSession = Session.getInstance(props);
Message msg = new MimeMessage(mailSession); //邮件信息
try
{
SMTPTransport st = (SMTPTransport)mailSession.getTransport(protocol);
st.connect(hostName, "***@sina.com", "******"); //***@sina.com为新浪帐号,******为密码
msg.setFrom(new InternetAddress("ping23.student@sina.com", "午刀十"));
msg.setSubject("今天天气真好");
msg.setContent("要下雨了", "text/html;charset=GBK");
InternetAddress[] iaRecevers = new InternetAddress[1];
iaRecevers[0] = new InternetAddress("***@sina.com"); //选择要接收的邮件地址
msg.setRecipients(Message.RecipientType.TO, iaRecevers);
st.sendMessage(msg, msg.getAllRecipients());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值