JavaMail programming with authentication

xiaoxiao2021-03-06  62

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 Javabean

Package NetCard;

/***Sendecard.java Send Mail's JavaBean * /

Import java.util.properties; import java.util.date; import javax.mail. *; import javax.mail.internet. *; import javax.activation. *;

Public class sendecard {

Private string tommail; private string subject; private string meanscard; string smtp = "****"; // Setting 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 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") ; // Set the mode for verification session session = session.getDefaultinstance (p, new email_ca ("user name", "password"))); mimeMessage msg = new mimeMessage (session); msg.setSentDate (new date ()) ; InternetAddress from = new InternetAddress (frommail); 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 (file path); // get to the attachment itself and into BodyPart mbp.setDataHandler ( New DataHandler (FDS)); // Get the file name is the same as Bodypart Mbp.SetFileName (fds.getname ()); msp.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 * 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 setPwd (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 )}} You can send mail to send email using free mailbox servers!

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

New Post(0)