java邮箱开发代码——发邮件

public class Demo1 {
	/**
	 * @param args
	 * @throws MessagingException 
	 */
	public static void main(String[] args) throws MessagingException {
		//第一种方式方式
		//send1();
		//第二种发送方式
		send2();
	}
	
	public static void send1()
	{
		try {
			/**
			 构建发送环境
			 */
			Properties properties = new Properties();
			properties.setProperty("mail.smtp.auth", "true");//接受认证
			properties.setProperty("mail.transport.protocol", "smtp");//设置发送协议
			
			Session session =Session.getDefaultInstance(properties);
			session.setDebug(true);//设置在控制台打印调试信息
			/**
			 * 构建邮件
			 */
			Message msg = new MimeMessage(session);
			msg.setText("逗你玩");		//发送内容
			msg.setFrom(new InternetAddress("xxxx@qq.com"));//设置发送邮件方地址
			
			/*
			构建发送类
			*/
			Transport transport = session.getTransport();
			transport.connect("smtp.qq.com", 25, "xxxxxx用户名", "zxxxx密码");//设置要连接的服务器地址、端口、用户名、密码
			transport.sendMessage(msg, new InternetAddress[]{new InternetAddress("13628303286@163.com")});//发送邮件给某些人
			transport.close();//关闭发送链接
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void send2() throws AddressException, MessagingException
	{
		/**
		 构建发送环境
		 */
		Properties properties = new Properties();
		properties.setProperty("mail.smtp.auth", "true");//接受服务器认证
		properties.setProperty("mail.transport.protocol", "smtp");//设置发送协议
		properties.setProperty("mail.host", "smtp.qq.com");//设置要连接的服务器地址,端口默认25
		
		Session session = Session.getInstance(properties,new Authenticator() {  //策略模式
			@Override
			protected PasswordAuthentication getPasswordAuthentication() { //返回用户名和密码
				// TODO Auto-generated method stub
				return new PasswordAuthentication("xxxx用户名", "xxxxxxx密码"); //设置用户名和密码
			}
		});
		session.setDebug(true); //显示调试信息
		/**
		 * 构建邮件
		 */
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("594389@qq.com"));//设置发送方地址
		msg.setSubject("中文主题");
		msg.setRecipients(RecipientType.TO, InternetAddress.parse("2399548@qq.com,13628303286@163.com,594389@qq.com")); //设置收件人的类型:TO:收件人;CC:抄送;BCC:暗送;和收件人
		msg.setContent("<span style='color:red'>中文呵呵</span>", "text/html;charset=gbk");//设置发送内容,以及内容的类型和编码
		
		/**
		 * 发送邮件
		 */
		Transport.send(msg);
	}
}


注:工程需要引入mail.jar包,如果运行环境低于jdk6还需引入activation.jar包

下载地址:http://download.csdn.net/user/zl594389970

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * JCatalog Project */ package com.hexiang.utils; import java.util.List; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.Properties; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.hexiang.exception.CatalogException; /** * Utility class to send email. * * @author <a href="380595305@qq.com">hexiang</a> */ public class EmailUtil { //the logger for this class private static Log logger = LogFactory.getLog("com.hexiang.util.EmailUtil"); /** * Send email to a single recipient. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param receiverAddress the recipient email address * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, String receiverAddress, String sub, String msg) throws CatalogException { List<String> recipients = new ArrayList<String>(); recipients.add(receiverAddress); sendEmail(smtpHost, senderAddress, senderName, recipients, sub, msg); } /** * Send email to a list of recipients. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param recipients a list of receipients email addresses * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, List<String> recipients, String sub, String msg) throws CatalogException { if (smtpHost == null) { String errMsg = "Could not send email: smtp host address is null"; logger.error(errMsg); throw new CatalogException(errMsg); } try { Properties props = System.getProperties(); props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null ); MimeMessage message = new MimeMessage( session ); message.addHeader("Content-type", "text/plain"); message.setSubject(sub); message.setFrom(new InternetAddress(senderAddress, senderName)); for (Iterator<String> it = recipients.iterator(); it.hasNext();) { String email = (String)it.next(); message.addRecipients(Message.RecipientType.TO, email); } message.setText(msg); message.setSentDate( new Date() ); Transport.send(message); } catch (Exception e) { String errorMsg = "Could not send email"; logger.error(errorMsg, e); throw new CatalogException("errorMsg", e); } } }
Java中发送邮件可以使用JavaMail API。下面是一个简单的Java程序示例,用于发送带有文本内容的邮件。 首先,需要导入javax.mail和javax.mail.internet包的类。 ```java import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { // 配置发送邮件的SMTP服务器信息 String host = "smtp.example.com"; final String username = "yourusername"; final String password = "yourpassword"; // 创建Properties对象,设置SMTP服务器信息 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); // 创建Session对象,用于发送邮件 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // 创建Message对象,设置收件人、发件人和邮件内容 Message message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("邮件主题"); message.setText("邮件内容"); // 发送邮件 Transport.send(message); System.out.println("邮件发送成功"); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 上述代码中,需要替换以下信息: - `host`:SMTP服务器的主机名。 - `username`:发件人的用户名。 - `password`:发件人的密码。 - `sender@example.com`:发件人的邮箱地址。 - `recipient@example.com`:收件人的邮箱地址。 你也可以根据需要,使用JavaMail API发送附件、HTML内容等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值