大家看到通过邮箱可以给朋友或其它人发送邮件,java api也提供了发送邮件的方法,下边就来个qq邮箱发送的小例子:
package com.zxr.utils; import java.io.UnsupportedEncodingException; 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.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; /** * 说明: 简单发送qq邮件程序 * @author 傲世狂少 * Date: 2012-04-14 */ public class EmailUtils { public static void sendEmail(String email, String content){ Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.qq.com"); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("发送者邮箱", "发送者邮箱密码"); } }); Message messge=new MimeMessage(session); try { messge.setFrom(InternetAddress.parse(MimeUtility.encodeText("傲世狂少")+" <xxxyyy@foxmail.com>")[0]); messge.setSubject("傲世狂少发送测试数据,请勿举报"); messge.setRecipient(Message.RecipientType.TO, new InternetAddress(email)); messge.setSentDate(new Date()); messge.setContent(content, "text/html;charset=gbk"); Transport.send(messge); } catch (MessagingException e) { e.printStackTrace(); }catch(UnsupportedEncodingException e1){ e1.printStackTrace(); } } public static void main(String []args){ sendEmail("接受者qq邮箱","傲世狂少追你很久了!"); } }?