JavaMail mail server

xiaoxiao2021-03-06  56

If you think there is no need to set the Mail server, and use JavaMail's mail to send features, the best way is to use your own free email server! These servers must be verified to log in, so JavaMail programming with authentication is required!

Description: 1. Download JavaMail.jar and Jaf Frame (handling multiple formats) 2. Use the mail server (requires authentication) 3.Sndecard.java sends Mail_ca with an attachment) with your existing free mailbox. java authentication javabeanpackage netcard; / *** SendEcard.java * send mail of javabean * / import java.util.Properties; import java.util.Date; import javax.mail *;. import javax.mail.internet *. ; import javax.activation *;. public class SendEcard {private String tomail; private String frommail; private String subject; private String content; private String imagecard; String smtp = "****"; // set the mail server public SendEcard ( ) {} public String getTomail () {return tomail;} public void setTomail (String tomail) {this.tomail = tomail;} public String getFrommail () {return frommail;} public void setFrommail (String frommail) {this.frommail = frommail;} public String getSubject () {return subject;} public void setSubject (String subject) {this.subject = subject;} public String getContent () {return content;} public void setContent (String content) {this.content = conte nt;} public String getImagecard () {return imagecard;} public void setImagecard (String imagecard) {this.imagecard = imagecard;} public boolean sendMail () {try {Properties p = System.getProperties (); p.put ( " Mail.smtp.host ", this.smtp); P.PUT (" mail.smtp.auth "," true "); // Setting to verify the mode session session = session.getDefaultInstance (p, new email_ca (" User name "," password ")); mimeMessage msg = new mimeMessage (session); msg.setSentDate (new Date ()); internetdress from = new internetdress (fire); msg.setFrom (from); internetAddress [] address =

{New InternetAddress (tomail)}; msg.setRecipients (Message.RecipientType.TO, address); msg.setSubject (this.subject); msg.setText (this.content); Multipart mp = new MimeMultipart (); MimeBodyPart mbp = New mimebodypart (); fileDataSource fds = new fileDataSource; // Get the attachment itself and incorporates Bodypart Mbp.SetDataHandler (New DataHandler (FDS)); // Get the file name is the same as Bodypart Mbp.SetFileName (FDS. GetName ()); mp.addbodypart (MP); Msg.SetContent (MP); Transport.send (MSG); Return true;} catch (addressexception addr_e) {system.out.println (addr_e.getMessage ()); Return False;} catch (messagingException msg_e) {system.out.println (msg_e.getMessage ()); Return False;}}} package netcard; / *** email_ca.java * identity authentication JavaBean * / Import Javax.mail. *; public class Email_ca extends javax.mail.Authenticator {private String user = null; private String pwd = null; public Email_ca () {super ();} public void setUser (String user) {this.user = user;} public Void SE tPwd (String pwd) {this.pwd = pwd;} public Email_ca (String user, String pwd) {super (); setUser (user); setPwd (pwd);} public PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (user, PWD);}}

To this you can use the free mailbox server for email!

转载请注明原文地址:https://www.9cbs.com/read-113341.html

New Post(0)