你在下边加一个新浪的邮件(没有就申请一个),天上用户名和密码!!
完整代码如下:
//标题
String message_subject=title;
//正文
String message_body="邮件正文内容!";
//JavaMail Session对象
Session psession;
//JavaMail Message对象
Message mesg;
Properties props=new Properties();
props.put("mail.smtp.host","smtp.sina.com.cn");//这里必须正确填入你的smtp(我这里添的是新浪的smtp)
psession=Session.getDefaultInstance(props,null);
psession.setDebug(true);
try
{
//创建一个邮件
mesg=new MimeMessage(psession);
//FROM地址
mesg.setFrom(new InternetAddress("xxx.sina.com"));
//TO地址
InternetAddress toAddress=new InternetAddress(message_recip);
mesg.addRecipient(Message.RecipientType.TO,toAddress);
//标题
mesg.setSubject(message_subject);
//正文
mesg.setText(message_body);
Transport transport = psession.getTransport("smtp");
transport.connect("smtp.sina.com.cn", "xxx","1234");//这里添一个你自己的邮件用户名和密码(假如为xxx.sina.com,密码为1234)
//最后发送邮件
transport.send(mesg);
}
catch(MessagingException ex)
{
while((ex=(MessagingException)ex.getNextException())!=null)
{
ex.printStackTrace();
}
}
完整代码如下:
//标题
String message_subject=title;
//正文
String message_body="邮件正文内容!";
//JavaMail Session对象
Session psession;
//JavaMail Message对象
Message mesg;
Properties props=new Properties();
props.put("mail.smtp.host","smtp.sina.com.cn");//这里必须正确填入你的smtp(我这里添的是新浪的smtp)
psession=Session.getDefaultInstance(props,null);
psession.setDebug(true);
try
{
//创建一个邮件
mesg=new MimeMessage(psession);
//FROM地址
mesg.setFrom(new InternetAddress("xxx.sina.com"));
//TO地址
InternetAddress toAddress=new InternetAddress(message_recip);
mesg.addRecipient(Message.RecipientType.TO,toAddress);
//标题
mesg.setSubject(message_subject);
//正文
mesg.setText(message_body);
Transport transport = psession.getTransport("smtp");
transport.connect("smtp.sina.com.cn", "xxx","1234");//这里添一个你自己的邮件用户名和密码(假如为xxx.sina.com,密码为1234)
//最后发送邮件
transport.send(mesg);
}
catch(MessagingException ex)
{
while((ex=(MessagingException)ex.getNextException())!=null)
{
ex.printStackTrace();
}
}
解决方案 »
- 新手问个XML小问题请高手指教
- 如何运算一段包含运算符的字符串 跪求啊
- java正则表达式匹配的问题
- 大家谁有好点的JSP分页?
- 一个很怪的问题?谁能解释一下?
- web高手进来看看~~
- 迷茫……,对以前没有web服务的web程序有所了解(请问tomcat)
- java.lang.NumberFormatException: 1 我的问题出在那里?
- 当访问量比较大时动态页面会造成效率严重下降吗?
- Base64转出图像为透明,求助
- jsp中关于ResultSet的问题?
- 动态下拉列表,如何取得下拉列表的<option value="<%=id%>"<%if(id==tradetype) {out.print("selected");}%> ><%=tradestr%></option>的
Props.put("mail.smtp.host","mail.sina.com.cn");
试试