java 发送邮件 配置_java mail使用qq邮箱发邮件的配置方法

程序入口:

Test_Email_N.java

import java.io.IOException;

import java.util.Date;

import java.util.Properties;

import javax.mail.Authenticator;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

public class Test_Email_N {

public static void  main(String args[]){

try {

send_email();

}catch (Exception e) {

e.printStackTrace();

}

}

public static void send_email() throws IOException, AddressException, MessagingException{

String to = "1219999@qq.com";

String subject = "subject";

String content = "content";

Properties properties = new Properties();

properties.put("mail.smtp.host", "smtp.qq.com");

properties.put("mail.smtp.port", "25");

properties.put("mail.smtp.auth", "true");

Authenticator authenticator = new Email_Authenticator("1219999@qq.com", "password");

javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);

MimeMessage mailMessage = new MimeMessage(sendMailSession);

mailMessage.setFrom(new InternetAddress("1219999@qq.com"));

// Message.RecipientType.TO属性表示接收者的类型为TO

mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

mailMessage.setSubject(subject, "UTF-8");

mailMessage.setSentDate(new Date());

// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象

Multipart mainPart = new MimeMultipart();

// 创建一个包含HTML内容的MimeBodyPart

BodyPart html = new MimeBodyPart();

html.setContent(content.trim(), "text/html; charset=utf-8");

mainPart.addBodyPart(html);

mailMessage.setContent(mainPart);

Transport.send(mailMessage);

}

}

其中依赖的jar包为javax.mail,我这里是maven管理的,直接用maven去下载jar包,也可以到https://java.net/projects/javamail/pages/Home直接下载jar包.

javax.mail

mail

1.5.0-b01

Email_Authenticator.java,这里继承了Authenticator 类,用来封装name,和password的:

package com.infomorrow.webtest.JuxinliTest.restdetect;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class Email_Authenticator extends Authenticator {

String userName = null;

String password = null;

public Email_Authenticator() {

}

public Email_Authenticator(String username, String password) {

this.userName = username;

this.password = password;

}

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName, password);

}

}

配置就这么多,把邮箱密码改成自己的就可以了,否则会报错。程序到这就可以运行了!

下面介绍的是配置properties文件来管理账号密码:

新建一个email.propertis文件。

email.propertis:

mail.smtp.host=smtp.qq.com

mail.smtp.port=25

username=1219999@qq.com

password=password

Test_Email.java 代码改为如下:

package com.infomorrow.webtest.JuxinliTest.restdetect;

import java.io.IOException;

import java.io.InputStream;

import java.util.Date;

import java.util.Properties;

import javax.mail.Authenticator;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

public class Test_Email {

public static void main(String args[]){

try {

send_email();

}catch (Exception e) {

e.printStackTrace();

}

}

public static void send_email() throws IOException, AddressException, MessagingException{

String to = "1215186706@qq.com";

String subject = "subject";//邮件主题

String content = "content";//邮件内容

Properties properties = new Properties();

InputStream resourceAsStream = null;

try {

resourceAsStream = Object.class.getResourceAsStream("/email.properties");

properties.load(resourceAsStream);

} finally{

if (resourceAsStream!=null) {

resourceAsStream.close();

}

}

System.err.println("properties:"+properties);

properties.put("mail.smtp.host", properties.get("mail.smtp.host"));

properties.put("mail.smtp.port", properties.get("mail.smtp.port"));

properties.put("mail.smtp.auth", "true");

Authenticator authenticator = new Email_Authenticator(properties.get("username").toString(), properties.get("password").toString());

javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);

MimeMessage mailMessage = new MimeMessage(sendMailSession);

mailMessage.setFrom(new InternetAddress(properties.get("username").toString()));

// Message.RecipientType.TO属性表示接收者的类型为TO

mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

mailMessage.setSubject(subject, "UTF-8");

mailMessage.setSentDate(new Date());

// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象

Multipart mainPart = new MimeMultipart();

// 创建一个包含HTML内容的MimeBodyPart

BodyPart html = new MimeBodyPart();

html.setContent(content.trim(), "text/html; charset=utf-8");

mainPart.addBodyPart(html);

mailMessage.setContent(mainPart);

Transport.send(mailMessage);

}

}

ok,到此为止。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java调用API发送企业邮箱,需要按照以下步骤操作: 1. 配置企业邮箱SMTP服务 首先需要在企业邮箱后台配置SMTP服务,获取SMTP服务器地址、端口号、用户名和密码等信息。一般情况下,SMTP服务器地址为smtp.exmail.qq.com,端口号为465或587。 2. 导入JavaMailJavaMail是一款Java邮件发送库,可以通过Maven或手动导入jar包的方式引入到项目中。 3. 编写Java代码 使用JavaMail发送邮件的代码示例: ``` public static void sendMail(String smtpServer, String smtpPort, String username, String password, String fromAddress, String toAddress, String subject, String content) throws MessagingException { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", smtpServer); props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.ssl.enable", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromAddress)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); message.setSubject(subject); message.setContent(content, "text/html;charset=UTF-8"); Transport.send(message); } ``` 其中,smtpServer、smtpPort、username和password为企业邮箱SMTP服务的配置信息;fromAddress和toAddress为发件人和收件人的邮箱地址;subject为邮件主题;content为邮件正文。 4. 调用Java代码 在Java程序中调用sendMail()方法即可发送邮件。 示例代码: ``` public static void main(String[] args) throws Exception { String smtpServer = "smtp.exmail.qq.com"; String smtpPort = "465"; String username = "your_email@your_company.com"; String password = "your_password"; String fromAddress = "your_email@your_company.com"; String toAddress = "recipient_email@recipient_company.com"; String subject = "Test Email"; String content = "<h1>Hello, World!</h1>"; sendMail(smtpServer, smtpPort, username, password, fromAddress, toAddress, subject, content); } ``` 以上就是使用Java调用API发送企业邮箱的步骤和代码示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值