邮件发送

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SimpleMailTest {
    public static void main(String[] args) throws EmailException {
        SimpleEmail email = new SimpleEmail();
        // 设置发送主机的服务器地址
        email.setHostName("smtp.163.com");
        // 设置收件人邮箱
        email.addTo("[email protected]", "wangyi163");
        // 发件人邮箱
        email.setFrom("[email protected]", "wangyi126");
        // 如果要求身份验证,设置用户名、密码,分别为发件人在邮件服务器上注册的用户名和密码
        email.setAuthentication("wangyi126", "wangyi126");
        // 设置邮件的主题
        email.setSubject("Hello, This is My First Email Application");
        // 邮件正文消息
        email.setMsg("I am bulktree This is JavaMail Application");
        email.send();
        System.out.println("The SimpleEmail send sucessful!!!");
    }
}
 

 

import java.net.MalformedURLException;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class AttachmentMailTest {
	public static void main(String[] args) throws EmailException,
			MalformedURLException {
		// 创建一个Email附件
		EmailAttachment emailattachment = new EmailAttachment();
		emailattachment.setPath("C:/text.txt");
		// emailattachment.setURL(new
		// URL("http://www.blogjava.net/bulktree/picture/bulktree.jpg"));
		emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
		emailattachment.setDescription("This is Smile picture");
		emailattachment.setName("wangyi163");
		// 创建一个email
		MultiPartEmail multipartemail = new MultiPartEmail();
		multipartemail.setHostName("smtp.163.com");
		multipartemail.addTo("[email protected]", "wangyi163");
		multipartemail.setFrom("[email protected]", "wangyi126");
		multipartemail.setAuthentication("wangyi126", "wangyi126");
		multipartemail.setSubject("This is a attachment Email");
		multipartemail.setMsg("this a attachment Eamil Test");
		// 添加附件
		multipartemail.attach(emailattachment);
		// 发送邮件
		multipartemail.send();
		System.out.println("The attachmentEmail send sucessful!!!");
	}
}

你可能感兴趣的:(apache,C++,c,.net,C#)