Das Beispiel in diesem Artikel beschreibt, wie JavaMail den E-Mail-Versand implementiert. Teilen Sie es als Referenz mit allen. Die Einzelheiten lauten wie folgt:
Laden Sie „activation.jar“ und „mail.jar“ herunter
Konfigurieren Sie CLASSPATH und fügen Sie die beiden oben genannten JAR-Pakete zu CLASSPATH hinzu.
JavaBean: „SendEmail.java“
package cls; import java.util.Properties; import javax.mail.*; String host; toAddr; String content; public SendEmail() { //Variable initialization = "smtp.qq.com"; "@"; } //E-Mail senden public boolean sendMail() { Properties prop; MimeMessage msg; // Verbindungsparameter speichern prop.put("mail.smtp.host") host); prop.put("mail.smtp.auth",true"); session = Session.getDefaultInstance(prop,null); // Holen Sie sich die Sitzung einer Mail msg = new MimeMessage(session); // E-Mail-Nachricht // Überprüfen Sie, ob die E-Mail-Adresse zulässig ist if(fromAddr == null || fromAddr.equals("")) { throw new Exception("Falsche Sendeadresse" } if(toAddr = = null ||. toAddr.equals("")) { throw new Exception("Destination address error" } //Setze die Quelladresse msg.setFrom(new InternetAddress(fromAddr)); //Legen Sie die Zieladresse fest msg.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddr)); //Legen Sie den Betreff fest msg.setSubject(subject); //E-Mail-Inhalt MimeBodyPart mbpContent = new MimeBodyPart (); mbpContent.setContent(content,"text/html"); E-Mail-Format mp.addBodyPart(mbpContent); msg.setSentDate(new Date()); // E-Mail senden Transport = session.getTransport("smtp"); .get("mail.smtp.host"),Benutzername,Passwort); transport.sendMessage(msg,msg.getRecipients(MimeMessage.RecipientType.TO)); transport.close(Exception e) { System.out.println(e); Getter und Setter public String getPassword() { return Password; host; } public void setHost(String host) { this.host = host; } public String getUsername() { return username } public void setUsername(String username) { this.username = username } public String getFromAddr() this.fromAddr; } public void setFromAddr(String addr) { fromAddr = addr } public String getToAddr() { return this.toAddr; } public void setToAddr(String addr) { toAddr = addr; } public String getSubject() { return subject; } public void setSubject(String sub) { subject = sub; } public String getContent() { return content; (String-Inhalt) { this.content = content;sendmail.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Mail</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <center> <jsp:useBean id="email" Scope="page" /> <jsp:setProperty name="email" property="toAddr" value="host" /> <jsp:setProperty name="email" property="fromAddr" value="host" /> <jsp:setProperty name="email " property="username" value="username" /> <jsp:setProperty name="email" property="password" value="password" /> <jsp:setProperty name="email" property="subject" value=" mail test" /> <jsp:setProperty name="email" property="content" value="hello world"/> <%=email.sendMail() %> </center> </body> </html>
Ich hoffe, dass dieser Artikel für die Java-Programmierung aller hilfreich sein wird.