一个stmp发送邮件的例子:ssl方式

使用java mail(jmail)通过gmail的stmp发送邮件:SSL方式
http://stackoverflow.com/questions/1990454/using-javamail-to-connect-to-gmail-smtp-server-ignores-specified-port-and-tries
Transport transport = session.getTransport("smtps");
transport.connect (smtp_host, smtp_port, smtp_username, smtp_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close(); 


public static boolean sendGmailEmail() {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "false");
		props.put("mail.smtp.port", Integer.parseInt("465"));
		props.put("mail.smtp.socketFactory.port", Integer.parseInt("465"));
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		// Session session = Session.getInstance(props);
		Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("formEmail@gmail.com", "77185201314");
			}
		});
		javax.mail.Message msg = new MimeMessage(session);
		try {
			msg.setFrom(new InternetAddress("formEmail@gmail.com"));
			msg.setSubject("Subject");
			Multipart mp = new MimeMultipart();
			MimeBodyPart mbp = new MimeBodyPart();
			mbp.setContent("This is a message.", "text/html;charset=UTF-8");
			mp.addBodyPart(mbp);
			msg.setContent(mp);
			Transport transport = session.getTransport();
			transport.connect("smtp.gmail.com", Integer.parseInt("465"), "formEmail@gmail.com", "123456");
			transport.sendMessage(msg, new Address[] { new InternetAddress("toEmail@qq.com") });
			transport.close();

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值