使用spring发送email

本文介绍了一种使用Java程序通过163邮箱SMTP服务发送邮件的方法。文中提供了发送邮件的具体步骤及代码示例,包括如何配置邮件服务器、设置邮件内容等。

发邮件的步骤:
1.账号开通smtp ( Simple Mail Transfer Protocol ) 服务,开通后会有个客户端登录密码或叫授权码,这里用的是163的授权码,注意,客户端登录用这个密码,如何开通smtp服务,自己去百度下,很容易,这里不再累述
2.上代码
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {

    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
        message.setTo(to);
        //message.setFrom(jHipsterProperties.getMail().getFrom());
        message.setFrom("xxxxx@163.com");
        message.setSubject(subject);
        message.setText(content, isHtml);
        //----------------------------------------
        javaMailSender.setHost("smtp.163.com");
        javaMailSender.setPort(25);
        javaMailSender.setUsername("xxxxxxxx@163.com");
        javaMailSender.setPassword("xxxxx");
        //--------------------------------------
        javaMailSender.send(mimeMessage);
        log.debug("Sent e-mail to User '{}'", to);
    } catch (Exception e) {
        log.warn("E-mail could not be sent to user '{}', exception is: {}", to, e.getMessage());
    }
}

说明:
参数列表to : 收件人邮箱,必须正确填写
subject:邮件主题
content:邮件正文
isMultipart:是否是multipart类型邮件,multipart的功能较多,不如添加附件
isHtml:正文是否是html类型

邮箱服务器地址不要填错,这里用的是163的,smtp.163.com
密码为开通smtp服务后的授权码

3.调用
@RequestMapping(value = “test”,
method = RequestMethod.POST)
public void test(){
System.out.println(“hello world”);
mailService.sendEmail(“XXXXX@qq.com”, “邮件测试”, “成功发送邮件———-“,false, false);
}

Spring框架中,可借助`JavaMailSender`来在`main`方法里实现邮件发送。下面给出一个示例代码,展示如何在`main`方法中运用Spring框架发送邮件: ```java import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import java.util.Properties; @Configuration class AppConfig { @Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.example.com"); // 设置SMTP服务器地址 mailSender.setPort(587); // 设置SMTP服务器端口 mailSender.setUsername("your_email@example.com"); // 设置发件人邮箱 mailSender.setPassword("your_email_password"); // 设置发件人邮箱密码 Properties props = mailSender.getJavaMailProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.debug", "true"); return mailSender; } } public class Main { public static void main(String[] args) { // 创建Spring上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); // 获取JavaMailSender实例 JavaMailSender mailSender = context.getBean(JavaMailSender.class); // 创建简单邮件消息 SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("your_email@example.com"); // 设置发件人邮箱 message.setTo("recipient_email@example.com"); // 设置收件人邮箱 message.setSubject("Test Email"); // 设置邮件主题 message.setText("This is a test email sent from Spring framework."); // 设置邮件正文 // 发送邮件 mailSender.send(message); // 关闭Spring上下文 context.close(); } } ``` 在这个示例中: 1. 首先定义了一个`AppConfig`类,使用`@Configuration`注解将其标记为配置类,并且定义了`javaMailSender`方法,该方法会创建并配置`JavaMailSender`实例。 2. 在`main`方法里,创建了`AnnotationConfigApplicationContext`来加载配置类。 3. 从Spring上下文中获取`JavaMailSender`实例。 4. 创建`SimpleMailMessage`对象,设置发件人、收件人、主题和正文。 5. 调用`mailSender.send(message)`方法来发送邮件。 6. 最后关闭Spring上下文。 需要注意,要把`smtp.example.com`、`your_email@example.com`、`your_email_password`和`recipient_email@example.com`替换成实际的SMTP服务器地址、发件人邮箱、发件人邮箱密码以及收件人邮箱。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值