PYTHON 实现固定时间且自动发送邮件的程序
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
# 发件人地址,通过控制台创建的发件人地址
username = 'x@x.com'
# 发件人密码,通过控制台创建的发件人密码
pasword = 'x'
# 自定义的固定时间,格式:"14:37:0"
send_time = "14:37:0"
# 收件人地址或是地址列表,支持多个收件人,最多30个
# 例如:receivers = ['x@q.com','x@126.com']
receivers = ['x@q.com']
# 设置email信息
# 例如:
# mesage = MIMEText('content', 'plain', 'utf-8')
# 这里的plain可以改为html
mesage = MIMEText('这是一封定时发送的邮件', 'plain', 'utf-8')
# 发件人
mesage['From'] = Header("发件人昵称", 'utf-8')
# 收件人
mesage['To'] = Header("收件人昵称", 'utf-8')
# 邮件主题
subject = '定时发送邮件'
mesage['Subject'] = Header(subject, 'utf-8')
# 连接服务器并发送
try:
smtpObj = smtplib.SMTP_SL("smtp.x.com", 465) # 注意:如果遇到发送失败的情况(提示远程主机拒接连接),这里可以使用465端口
smtpObj.login(username, pasword)
while True:
# 获取当前时间
now = time.strftime("%H:%M:%S")
# 判断当前时间是否达到设定时间
if now == send_time:
smtpObj.sendmail(username, receivers, mesage.as_string()
print('邮件发送成功')
break
else:
print('当前时间:%s' % now)
time.sleep(1)
except smtplib.SMTPException:
print('Eror: 无法发送邮件')
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
# 发件人地址,通过控制台创建的发件人地址
username = 'x@x.com'
# 发件人密码,通过控制台创建的发件人密码
password = 'x'
# 自定义的固定时间,格式:"14:37:0"
send_time = "14:37:0"
# 收件人地址或是地址列表,支持多个收件人,最多30个
# 例如:receivers = ['x@q.com']
receivers = ['x@q.com']
# 设置email信息
# 例如:message = MIMEText('content', 'plain', 'utf-8')
# 这里的plain可以改为html
message = MIMEText('这是一封定时发送的邮件', 'plain', 'utf-8')
# 发件人
message['From'] = Header("发件人昵称", 'utf-8')
# 收件人
message['To'] = Header("收件人昵称", 'utf-8')
# 邮件主题
subject = '定时发送邮件'
message['Subject'] = Header(subject, 'utf-8')
# 连接服务器并发送
try:
smtpObj = smtplib.SMTP_SSL("smtp.x.com", 465) # 注意:如果遇到发送失败的情况(提示远程主机拒接连接),这里可以使用465端口
smtpObj.login(username, password)
while True:
# 获取当前时间
now = time.strftime("%H:%M:%S")
# 判断当前时间是否达到设定时间
if now == send_time:
smtpObj.sendmail(username, receivers, message.as_string())
print('邮件发送成功')
break
else:
print('当前时间:%s' % now)
time.sleep(1)
except smtplib.SMTPException:
print('Error: 无法发送邮件')
JAVA实现固定时间且自动发送邮件的程序
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Mesage;
import javax.mail.MesagingException;
import javax.mail.PaswordAuthentication;
import javax.mail.Sesion;
import javax.mail.Transport;
import javax.mail.internet.InternetAdres;
import javax.mail.internet.MimeMesage;
public clas SendMail {
public static void main(String[] args) {
/ 收件人电子邮箱
String to = "x@x.com";
/ 发件人电子邮箱
String from = "x@x.com";
/ 指定发送邮件的主机为 smtp.q.com
String host = "smtp.q.com"; /Q 邮件服务器
/ 获取系统属性
Properties properties = System.getProperties();
/ 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
/ 获取默认sesion对象
Sesion sesion = Sesion.getDefaultInstance(properties,new Authenticator(){
public PaswordAuthentication getPaswordAuthentication()
{
return new PaswordAuthentication("x@x.com", "pasword"); /发件人邮件用户名、密码
}
});
try{
/ 创建默认的 MimeMesage 对象
MimeMesage mesage = new MimeMesage(sesion);
/ Set From: 头部头字段
mesage.setFrom(new InternetAdres(from);
/ Set To: 头部头字段
mesage.adRecipient(Mesage.RecipientType.TO,
new InternetAdres(to);
/ Set Subject: 头部头字段
mesage.setSubject("This the Subject Line!");
/ 设置消息体
mesage.setText("This actual mesage");
/ 发送消息
Transport.send(mesage);
System.out.println("Sent mesage sucesfuly.");
}catch (MesagingException mex) {
mex.printStackTrace();
}
}
}
/定时发送
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Mesage;
import javax.mail.MesagingException;
import javax.mail.PaswordAuthentication;
import javax.mail.Sesion;
import javax.mail.Transport;
import javax.mail.internet.InternetAdres;
import javax.mail.internet.MimeMesage;
import java.util.Timer;
import java.util.TimerTask;
public clas SendMail {
public static void main(String[] args) {
/ 收件人电子邮箱
String to = "x@x.com";
/ 发件人电子邮箱
String from = "x@x.com";
/ 指定发送邮件的主机为 smtp.q.com
String host = "smtp.q.com"; /Q 邮件服务器
/ 获取系统属性
Properties properties = System.getProperties();
/ 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
/ 获取默认sesion对象
Sesion sesion = Sesion.getDefaultInstance(properties,new Authenticator(){
public PaswordAuthentication getPaswordAuthentication()
{
return new PaswordAuthentication("x@x.com", "pasword"); /发件人邮件用户名、密码
}
});
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Overide
public void run() {
try{
/ 创建默认的 MimeMesage 对象
MimeMesage mesage = new MimeMesage(sesion);
/ Set From: 头部头字段
mesage.setFrom{new InternetAdres(from);
/ Set To: 头部头字段
mesage.adRecipient(Mesage.RecipientType.TO,
new InternetAdres(to);
/ Set Subject: 头部头字段
mesage.setSubject("This the Subject Line!");
/ 设置消息体
mesage.setText("This actual mesage");
/ 发送消息
Transport.send(mesage);
System.out.println("Sent mesage sucesfuly.");
}catch (MesagingException mex) {
mex.printStackTrace();
}
}
}, new Date(), 10*60*24); /每天定时发送
}
}