使用JavaMail发送邮件需要以下步骤:
步骤一:添加JavaMail依赖
在项目中添加JavaMail的依赖包,例如使用Maven可以添加以下依赖:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
步骤二:设置邮件发送配置
JavaMail需要用到邮箱账号和密码进行邮件发送,因此需要设置邮件发送的配置信息,如下:
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true"); // TRUE表示需要认证
props.put("mail.smtp.starttls.enable", "true"); // 开启SSL加密
props.put("mail.transport.protocol", "smtp");
其中,mail.smtp.host
表示SMTP主机地址,这里以QQ邮箱为例;mail.smtp.port
表示SMTP端口,QQ邮箱的SMTP端口为587; mail.smtp.auth
表示是否需要认证,需要设置为true
;mail.smtp.starttls.enable
表示启用SSL加密,需要启用。
步骤三:创建Session对象
Session对象代表邮件会话,需要指定邮箱账号和密码进行认证,如下:
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your_email@qq.com", "your_password");
}
});
这里将邮箱账号和密码传入PasswordAuthentication
对象中,通过Authenticator
进行认证,创建Session
会话对象。
步骤四:创建邮件
创建邮件需要设置收件人、发件人、主题、内容,还可以设置附件等信息,如下:
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_email@qq.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient_email@example.com"));
message.setSubject("Test Email");
// 设置邮件正文
message.setContent("<h1>Hello JavaMail</h1>", "text/html;charset=utf-8");
这里创建了一个MimeMessage
对象,设置发件人、收件人、主题以及邮件正文内容。
步骤五:发送邮件
构建好邮件之后,调用Transport
类的send
方法发送邮件:
Transport.send(message);
至此,就完成了JavaMail发送邮件的过程。以下是一个完整的代码示例:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your_email@qq.com", "your_password");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_email@qq.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient_email@example.com"));
message.setSubject("Test Email");
// 设置邮件正文
message.setContent("<h1>Hello JavaMail</h1>", "text/html;charset=utf-8");
// 发送邮件
Transport.send(message);
}
}
这个示例演示了如何使用QQ邮箱发送邮件。需要把代码中的"your_email@qq.com"
和"your_password"
替换成自己的邮箱账号和密码,把"recipient_email@example.com"
替换成收件人的邮箱地址。邮件正文可以自己修改,这里只是一个简单的示例。发送邮件之后,可以登录QQ邮箱查看是否收到了邮件。
以下是另外一个示例,演示如何在邮件中添加附件:
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class SendEmailWithAttachment {
public static void main(String[] args) throws MessagingException {
//邮箱配置
final Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtp");
//邮箱认证
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your_email@qq.com", "your_password");
}
});
//邮件内容
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("your_email@qq.com"));
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient_email@example.com"));
mimeMessage.setSubject("Email with attachment");
//邮件正文
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent("This is the content of the email with attachment", "text/plain");
//附件
MimeBodyPart mimeAttachment = new MimeBodyPart();
DataSource source = new FileDataSource("file_path"); //文件路径
mimeAttachment.setDataHandler(new DataHandler(source));
mimeAttachment.setFileName("file_name"); //文件名称
//构建邮件内容
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(mimeBodyPart); //添加正文
mimeMultipart.addBodyPart(mimeAttachment); //添加附件
//设置邮件内容
mimeMessage.setContent(mimeMultipart);
//发送邮件
Transport.send(mimeMessage);
}
}
这个示例演示了如何在邮件中添加附件,需要修改代码中的"your_email@qq.com"
和"your_password"
为自己的邮箱账号和密码,把"recipient_email@example.com"
替换成收件人的邮箱地址,把"file_path"
替换成需要添加的附件的文件路径,把"file_name"
替换成自定义的文件名。发送完之后,可以登录邮箱查看是否成功发送了包含附件的邮件。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何使用JavaMail发送邮件 - Python技术站