通过SMTP发邮件

package email;

import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Email {

	public static void main(String[] args) {

		if (args.length < 4) {
			return;
		}
	//////////////////////////////////////////////////////////////////
	//parameter 1:email address
	//e.g [email protected],[email protected]
	//parameter 2:the path of attachment
	//parameter 3:email context
	//parameter 3:email title
	//////////////////////////////////////////////////////////////////

		Email email = new Email();
		email.CreateEmail(args);

	}

	public void CreateEmail(String[] args) {
		try {
			Properties properties = new Properties();
			// properties.setProperty("mail.smtp.auth", "true");
			properties.setProperty("mail.transport.protocol", "smtp");
			properties.setProperty("mail.smtp.host", "server");
			properties.setProperty("mail.smtp.port", "25");
			Session session = Session.getInstance(properties, null);
			session.setDebug(false);

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("My Service <[email protected]>"));

			message.setSubject(args[3]);

			message.setRecipients(RecipientType.TO,InternetAddress.parse(args[0]));

			MimeBodyPart bodyPartAttch = createAttachMent(args[1]);
			MimeBodyPart bodyPartContentAndPic = createContent(args[2]);
			MimeMultipart mimeMuti = new MimeMultipart("mixed");
			mimeMuti.addBodyPart(bodyPartAttch);
			mimeMuti.addBodyPart(bodyPartContentAndPic);
			message.setContent(mimeMuti);
			message.saveChanges();

			Transport.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
			System.out.println("Email List:"+args[0]);
			System.out.println("Attachment Path:"+args[1]);
			System.out.println("Content:"+args[2]);
		}

	}

	public MimeBodyPart createAttachMent(String path) throws MessagingException {
		MimeBodyPart mimeBodyPart = new MimeBodyPart();
		FileDataSource dataSource = new FileDataSource(new File(path));
		mimeBodyPart.setDataHandler(new DataHandler(dataSource));
		mimeBodyPart.setFileName(dataSource.getName());
		return mimeBodyPart;
	}

	public MimeBodyPart createContent(String content) throws MessagingException {
		MimeMultipart mimeMutiPart = new MimeMultipart("related");

		MimeBodyPart contentBodyPart = new MimeBodyPart();

		contentBodyPart.setContent("<br>  </br><br>" + content + "</br>",
				"text/html;charset=gbk");
		mimeMutiPart.addBodyPart(contentBodyPart);

		MimeBodyPart allBodyPart = new MimeBodyPart();
		allBodyPart.setContent(mimeMutiPart);
		return allBodyPart;
	}
}

时间: 2024-08-05 18:13:22

通过SMTP发邮件的相关文章

CSHOP后台设置SMTP发邮件提示 Error: need RCPT command 错误解决

其实错误原因并不是因为此错误,经检测,邮件服务器返回的真实错误是 501 mail from address must be same as authorization user .只因为同时返回了 503 Error: need MAIL command 和 503 Error: need RCPT command ,而ECSHOP只提示了最后一行错误. <br>220 smtp.qq.com Esmtp QQ Mail Server<br>250-smtp.qq.com250-

asp.net中模拟测试smtp发邮件

最近在编程人生里要测试一个会员邮件的功能,就写了下面的代码. 在asp.net 中,有时要测试发信SMTP,但如果在单元测试中,如果没方便好用的 smtp怎么办,其实还是有办法模拟的,下面讲解下: 在web.config 中设置 <system.net>   <mailSettings>      <smtp deliveryMethod="SpecifiedPickupDirectory">          <specifiedPickup

ubuntu和centos下使用外部smtp发邮件

centos上: 第一步:在qq邮箱设置-->账户中开启pop3/smtp服务,并设置独立密码 第二步:yum install -y mailx 第三步:vim /etc/mail.rc 在文件最后添加你的邮箱信息: set [email protected]set smtp=smtp.qq.comset smtp-auth-user=82312924set smtp-auth-password=Your passwordset smtp-auth-login 第四步:就能直接使用echo &quo

Linux使用mailx通过外部smtp发邮件

利用mailx发邮件的功能可以做很多事,通常可以通过脚本让主机判断运行状态给指定邮箱发邮件. ##环境:虚拟机,CentOS7 通过yum安装: yum install -y mailx yum安装非常简单,其中要注意的是mailx版本最好大于7,redhat5系列之前自带的mailx发邮件需要通过sendmail程序来发送,所以需要更新版本 源码包安装:mailx-12.4 cd #回到家目录 tar xvjf mailx-12.4.tar.bz2 && cd mailx-12.4.ta

Python_014 SMTP发邮件

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一种很方便的途径发送电子邮件.它对smtp协议进行了简单的封装. Python创建 SMTP 对象语法如下: import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) 参数说明: host: SMTP 服务器

【C#学习笔记】smtp发邮件

using System; using System.Net; using System.Net.Mail; using System.Text; namespace ConsoleApplication { class Program { static void Main(string[] args) { MailMessage mail = new MailMessage(); mail.From = new MailAddress("***@gmail.com"); mail.T

Linux下配置使用外部SMTP发邮件

[[email protected] /]# mail -s "Message"  [email protected] < /tmp/info.message 系统默认以"[email protected]主机名" 发送邮件,收到邮件直接被过虑成垃圾邮件了,那可不可以自定义邮箱发送邮件呢? 答案肯定可以的. 下载mailx包 mailx官方站点 http://heirloom.sourceforge.net/ 下载最新版本mailx-12.4.tar.bz2

C#SMTP发邮件

public static bool SendMailUse() { string host = "smtp.163.com";// 邮件服务器smtp.163.com表示网易邮箱服务器 string userName = "[email protected]";// 发送端账号 string password = "a96606";// 发送端密码(这个客户端重置后的密码) SmtpClient client = new SmtpClient(

python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送)

SMTP发送邮件的时候,并没有特殊的通信语句告诉邮件服务器 谁是主送,谁是抄送/密送,这三个角色都是以同样的方式告诉邮件服务器的,然后重点在邮件内容里. 邮件内容分为头和体两部分(就像http),头部包含了各种meta信息,其中说明了谁要用to,谁要cc,谁要bcc. 一个典型的带to和bcc的邮件发送过程debug日志如下: send: 'ehlo E42.lan\r\n' reply: b'250-smtp.qq.com\r\n' reply: b'250-PIPELINING\r\n' r