Are catching up every year, and children ask me how to send emails in Java programs, especially in HTML format, put an example here:
MAVEN introduces JavaMail
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
and write a simple tool class.
Package com.xxx.tools;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import javax.mail.internet.MimeUtility;
public class Htmlemail {private String smtpserver;
Private String username;
private String password; public void SendMessage (string to, string subject, String messagetext) throws Messagingexception,
java.io.UnsupportedEncodingException {java.util.Properties props = new Java.util.Properties (); Props.setproperty ("Mail.smtp.auth", "true");//Specify whether SMTP authentication Props.setproperty ("Mail.smtp.host", smtpserver) is required;//Specify SM
TP Server Props.put ("Mail.transport.protocol", "SMTP");
Session mailsession = session.getdefaultinstance (props);
Mailsession.setdebug (TRUE)//Whether debug information is displayed on the console internetaddress fromaddress = new internetaddress (username); InternetAddress toaddress = new Internetaddress (to);
MimeMessage emailmessage = new MimeMessage (mailsession);
Emailmessage.setfrom (fromaddress);
Emailmessage.addrecipient (Javax.mail.Message.RecipientType.TO, toaddress);
Emailmessage.setsentdate (New Java.util.Date ());
Emailmessage.setsubject (Mimeutility.encodetext (Subject, "Utf-8", "B"));
Emailmessage.setcontent (MessageText, "text/html;charset=utf-8");
Transport transport = Mailsession.gettransport ("SMTP");
Transport.connect (smtpserver, username, password);
Transport.sendmessage (Emailmessage, emailmessage.getallrecipients ());
Transport.close ();
Public String Getsmtpserver () {return smtpserver;
} public void Setsmtpserver (String smtpserver) {this.smtpserver = SmtpServer;
Public String GetUserName () {return username;
} public void Setusername (String username) {this.username = username; } public String GETPASSWOrd () {return password;
} public void SetPassword (String password) {this.password = password; }
}
And then configure it in spring
<bean id= "Htmlmail" class= "Com.xxx.tools.HtmlEmail" >
<property name= "smtpserver" value= "smtp.qq.com"/ >
<property name= "username" value= "1xxxx7@qq.com" ></property>
<property name= "Password" Value= "xxxxxxx" ></property>
</bean>
Then use it where you want to use it:
@Controller
@RequestMapping ("/user") public
class Userregcontroller {
@Resource
private Htmlemail email;
@RequestMapping ("/reg") public
void SendMail (@RequestParam ("username") String username, @RequestParam ("Userpass ") string userpass, @RequestParam (" email ") string email_addr, HttpServletRequest request) {
string valid_string = req Uest.getsession (True). GetId ();
String Valid_url = "<a href= ' http://localhost:8080/user/valid?username=" + username + "&validstring=" + valid_ String + "' target= ' _blank ' > Click this link to activate account </a>";
try {
email.sendmessage (email_addr, "user activated", valid_url);
} catch (Messagingexception e) {
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
e.printstacktrace ();
}
}