发表于: 2021-04-08 22:58:16
1 1607
今天完成的事情:
把之前使用的qq邮箱更换成阿里云邮箱,购买域名
测试上传图片到本地
明天计划的事情:
图片迁移
深度思考
遇到的问题:
收获:
阿里云邮箱:
public static int SendMail(String email, String code) {
logger.info("发送邮件!!");
int x = 0;
// 配置发送邮件的环境属性
final Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.host", ALIDM_SMTP_HOST);
props.put("mail.smtp.socketFactory.class", CLASS);
props.put("mail.smtp.socketFactory.port", SOCKETFACTORY_PORT);
props.put("mail.smtp.port", PORT);
// 发件人的账号
props.put("mail.user", FROM);
// 访问SMTP服务时需要提供的密码
props.put("mail.password", PASSWORD);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// mailSession.setDebug(true);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
try {
// 设置发件人
InternetAddress from = new InternetAddress(FROM);
message.setFrom(from);
Address[] a = new Address[1];
a[0] = new InternetAddress(FROM);
message.setReplyTo(a);
// 设置收件人
InternetAddress to = new InternetAddress(email);
message.setRecipient(MimeMessage.RecipientType.TO, to);
// 设置邮件标题
message.setSubject("验证邮件");
StringBuilder s = new StringBuilder();
s.append(code);
logger.error(s);
message.setContent(s.toString(), "text/html;charset=UTF-8");
Transport.send(message);
} catch (MessagingException e) {
System.out.println(e);
logger.info("发送邮件失败,错误是:" + e);
String err = e.getMessage();
// 在这里处理message内容, 格式是固定的
System.out.println(err);
}
return x;
}
测试上传图片到本地
public static String upload(MultipartFile pictureFile) throws IOException {
// 装配图片地址
String imgPath = null;
// 判断pictureFile不为空,则上传图片
if (pictureFile != null) {
// 设置图片上传路径,我是将图片存在本地的文件夹中
// String path = "D:\JAVASourceCode\SpringMVC-Task7\src\main\webapp\images123";
String path = "/usr/local/tomcat/apache-tomcat-9.0.40/webapps/";
// 获取上传的文件名全称
String fileName=pictureFile.getOriginalFilename();
// 获取上传文件的后缀名
String suffix=fileName.substring(fileName.lastIndexOf("."));
// 使用 UUID 给图片重命名,并去掉四个“-”
String newFileName = UUID.randomUUID().toString().replaceAll("-", "")+suffix;
// 检验文件夹是否存在,不存在则创建一个文件夹,即路径必须正确
isFolderExists(path);
// 将重名命后的图片上传到图片上传路径
pictureFile.transferTo(new File(path, newFileName));
// 装配后的图片地址,包含图片名称、后缀,若使用nginx代理,则不加虚拟路径
imgPath = newFileName;
}
评论