天学习了如何用Java[1] 发送普通邮件,HTML邮件和带附件的邮件。黑客最喜欢干的事情就是发钓鱼邮件,安全砖家还为其美名叫鱼叉攻击,啧啧。
ailx10:网络安全优秀回答者,网络安全硕士
发送一封简单的QQ邮件:
package hackbiji;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class hello {
public static void main(String[] args) {
String to = "792161993@qq.com";
String from = "393803933@qq.com";
String host = "smtp.qq.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host);
properties.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("393803933","***********");
}
});
try {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
mimeMessage.setSubject("主题:黑客笔记");
mimeMessage.setText("正文:我是ailx10,我喜欢2进制安全和渗透技术");
Transport.send(mimeMessage);
System.out.println("发送邮件成功");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
发送一封HTML QQ邮件:
package hackbiji;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class hello {
public static void main(String[] args) {
String to = "792161993@qq.com";
String from = "393803933@qq.com";
String host = "smtp.qq.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host);
properties.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("393803933@qq.com","************");
}
});
try {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
mimeMessage.setSubject("主题:黑客笔记");
mimeMessage.setContent("<img src=\"http://hackbiji.top/images/avatar.jpg\" width=\"50\" height=\"50\" />","text/html");
Transport.send(mimeMessage);
System.out.println("发送邮件成功");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
发送一封带附件的QQ邮件
package hackbiji;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;
public class hello {
public static void main(String[] args) {
String to = "792161993@qq.com";
String from = "393803933@qq.com";
String host = "smtp.qq.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host);
properties.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("393803933@qq.com","***********");
}
});
try {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
mimeMessage.setSubject("主题:黑客笔记");
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setText("正文:我是ailx10");
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(msgBodyPart);
msgBodyPart = new MimeBodyPart();
String filepath = "C:\\Users\\Admin\\IdeaProjects\\helloworld\\src\\src\\main\\java\\hackbiji\\hack.zip";
String filename = "hack.zip";
FileDataSource fileDataSource = new FileDataSource(filepath);
msgBodyPart.setDataHandler(new DataHandler(fileDataSource));
msgBodyPart.setFileName(filename);
mimeMultipart.addBodyPart(msgBodyPart);
mimeMessage.setContent(mimeMultipart);
Transport.send(mimeMessage);
System.out.println("发送邮件成功");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
言
欢迎再次回到我们的Python邮件发送系列文章!在上一篇文章中,我们成功搭建了使用Python发送邮件的基础环境,并学会了如何连接到SMTP服务器。今天,我们将继续深入,学习如何编写一封完整的邮件内容,并通过Python将其发送出去。
一、创建邮件内容
在Python中,email模块提供了丰富的类和方法,让我们能够轻松地构建邮件内容。对于简单的文本邮件,我们可以使用email.mime.text.MIMEText类。
from email.mime.text import MIMEText
# 邮件正文内容
message_text = "你好,这是一封来自Python的邮件!希望你喜欢Python编程的乐趣。"
# 创建邮件对象
# 注意:这里的'plain'表示纯文本,'utf-8'是字符编码
message = MIMEText(message_text, 'plain', 'utf-8')
# 设置邮件的头部信息
# 发件人、收件人和邮件主题都是邮件头部的重要部分
message['From'] = 'your_email@gmail.com' # 请替换成你的邮箱地址
message['To'] = 'receiver_email@example.com' # 请替换成接收者的邮箱地址
message['Subject'] = 'Hello from Python!' # 邮件主题
二、发送邮件
现在,我们已经有了邮件内容(包括正文和头部信息),接下来就是通过SMTP服务器发送邮件了。我们将复用之前文章中创建的SMTP连接逻辑,来完成发送任务。
import smtplib
# SMTP服务器和端口(以Gmail为例)
smtp_server = 'smtp.gmail.com'
port = 587
# 你的Gmail账号和密码(或应用专用密码)
email = 'your_email@gmail.com' # 请替换成你的邮箱地址
password = 'your_app_specific_password' # 请替换成你的Gmail应用专用密码
try:
# 创建一个SMTP连接
server = smtplib.SMTP(smtp_server, port)
server.starttls() # 启用TLS加密
server.login(email, password) # 登录
# 发送邮件
# 注意:sendmail方法的第一个参数是发件人邮箱,第二个参数是接收者邮箱列表(即使只有一个接收者也要用列表形式),第三个参数是邮件内容对象
server.sendmail(email, [message['To']], message.as_string())
print("邮件发送成功!")
finally:
# 关闭SMTP连接
server.quit()
注意:
结语
恭喜你,现在你已经学会了如何使用Python编写并发送一封简单的文本邮件!在下一篇文章中,我们将进一步探索如何发送HTML邮件和带附件的邮件,让你的邮件内容更加丰富多彩。请继续关注我们的系列文章,不要错过任何精彩内容!
pringBoot整合了Java Mail可以很方便的发送电子邮件。
我们来看看如何发送HTML格式的电子邮件。
在SpringBoot的pom文件中导入电子邮件的starter。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在application.properties中配置邮箱信息,包括邮件服务器地址、用户名和密码。
spring.mail.host=smtp.qq.com
spring.mail.username=123@qq.com
spring.mail.password=456
使用@Autowired注解注入JavaMailSender对象。
@Autowired
private JavaMailSender javaMailSender;
创建mimeMessage对象发送HTML邮件。
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
try {
helper.setFrom("123@qq.com(帅锅)");
helper.setTo("456@qq.com");
helper.setSubject("你好");
helper.setText("<a href='https://www.qq.com'>点我</a>",true);
} catch (MessagingException e) {
e.printStackTrace();
}
通过MimeMessageHelper对象设置邮件信息。
setText第一个参数是邮件的正文,在这里输入HTML代码
setText第二个参数是设置是否是HTML邮件,要设置为true
方法 | 作用 |
helper.setFrom | 设置发件人地址,可以通过“()”设置别名 |
helper.setTo | 设置收件人地址 |
helper.setSubject | 设置邮件标题 |
helper.setText | 设置邮件正文,第二个参数设置是否为HTML邮件 |
通过 javaMailSender.send方法发送电子邮件,参数是构建的mimeMessage对象。
@Component
public class MailServiceImpl implements MailService{
@Autowired
private JavaMailSender javaMailSender;
@Override
public void sendMail() {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
try {
helper.setFrom("123@qq.com(帅锅)");
helper.setTo("456@qq.com");
helper.setSubject("你好");
helper.setText("<a href='https://www.qq.com'>点我</a>",true);
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(mimeMessage);
}
}
通过测试方法测试邮件发送。
@SpringBootTest
public class MailTest {
@Autowired
private MailService mailService;
@Test
void test(){
mailService.sendMail();
}
}
邮箱收到了发送的邮件,连接是可以点击的,是一个HTML格式的邮件。
*请认真填写需求信息,我们会在24小时内与您取得联系。