java发送邮件

引入bean




    
    
    
    
    
     
            
                true
                true
                465
                javax.net.ssl.SSLSocketFactory
            
        


java实现


import java.io.File;
import java.util.List;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

public class MailUtil {

private static final LoggerLOGGER = LoggerFactory.getLogger(MailUtil.class);

    @Autowired
    JavaMailSenderImpl     mailSender;

    public void sendSimpleMail(String title, String content, String address) {

        SimpleMailMessage mail =new SimpleMailMessage();
        mail.setFrom(mailSender.getUsername());
        mail.setTo(address);
        mail.setSubject(title);
        mail.setText(content);
        mailSender.send(mail);
    }

public StringsendHtmlMailWithVelocity(final String title, final String content, final String[] receiver, final String[] ccReceiver, final List picList) {

        MimeMessagePreparator messagePreparator =new MimeMessagePreparator() {

        @Override
        public void prepare(MimeMessage mimeMessage)throws Exception {

        MimeMessageHelper message =new MimeMessageHelper(mimeMessage, true, "UTF-8");

         message.setFrom(new InternetAddress(mailSender.getUsername(), "显示别名", "UTF-8"));
         message.setTo(receiver);
         message.setCc(ccReceiver); 
         }

        message.setSubject(title);
        message.setText(content, true);

                for (int i =0; i
                    //从本地文件夹中获取所需图片
                    FileSystemResource res =new FileSystemResource(new File(picList.get(i)));
                    message.addInline(String.valueOf(i), res);
                }
        }
        };

        try {
           mailSender.send(messagePreparator);
        }catch (Exception e) {
        return e.getMessage();

        }
    return "success";
 }


public StringsendHtmlMail(final String title, final String content, final String [] receiver, final String[] ccReceiver) {

MimeMessagePreparator messagePreparator =new MimeMessagePreparator() {

            @Override
            public void prepare(MimeMessage mimeMessage)throws Exception {

            MimeMessageHelper message =new MimeMessageHelper(mimeMessage, true, "UTF-8");

            message.setFrom(new InternetAddress(mailSender.getUsername(), "显示别名", "UTF-8"));
            message.setTo(receiver);
            message.setCc(ccReceiver);

                }
               message.setSubject(title);
               message.setText(content, true);
            }
};

        try {
            mailSender.send(messagePreparator);
        }catch (Exception e) {

        return e.getMessage();

        }

return "success";

    }

}

你可能感兴趣的:(java发送邮件)