In the case of simple email sending with file upload, this case has to be imported into the mail. jar package, which can be downloaded on Baidu.
Note the following:
①: Name --> it's just a name. Do not include @ or anything,
②: Smtp address: QQ mailbox --> smtp.qq.com
163 email --> smtp.163.com
126 email --> smtp.126.com
Yeah email --> smtp.yeah.net
That is, smtpServer Parameters
Package com. email;
Import javax. mail .*;
Import javax. mail. internet .*;
Import java. util .*;
Import javax. activation .*;
Public class BaseMail {
Private int count = 1;
Transport transport = null;
/*** Send an email
* @ Param smtp server example: smtp.exmail.qq.com
* @ Param User Name
* @ Param Password
* @ Param sender's email address
* @ Param recipient's email address
* @ Param mail title
* @ Param Message Body
* @ Param attachment address
*/
Public boolean sendMail (String smtpServer, String name, String password, String meMail, String toMail, String mailTitle, String mailText String fileName ){
Long begin = System. currentTimeMillis ();
Properties props = System. getProperties (); // set the smtp server
Props. setProperty ("mail. smtp. host", smtpServer); // Most smpts need to be verified.
Props. put ("mail. smtp. auth", "true ");
Session s = Session. getInstance (props); // to view runtime information
// Create a message object by the mail session
MimeMessage message = new MimeMessage (s );
Try {
// Sender
InternetAddress from = new InternetAddress (meMail );
Message. setFrom (from );
// Recipient
InternetAddress to = new InternetAddress (toMail );
Message. setRecipient (Message. RecipientType. TO, );
// Mail title
Message. setSubject (mailTitle );
// String content = "Test content ";
// The Mail content, or the plain text "text/plain"
// Message. setContent (content, "text/html; charset = GBK ");
// The following code sends an attachment
// String fileName = "d: // hello.txt ";
MimeBodyPart messageBodyPart = new MimeBodyPart ();
MessageBodyPart. setText (mailText );
Multipart multipart = new MimeMultipart ();
Multipart. addBodyPart (messageBodyPart );
MessageBodyPart = new MimeBodyPart ();
// Upload a file
If (null! = FileName ){
DataSource source = new FileDataSource (fileName );
MessageBodyPart. setDataHandler (new DataHandler (source ));
MessageBodyPart. setFileName (fileName. substring (fileName. lastIndexOf ("/") + 1 ));
Multipart. addBodyPart (messageBodyPart );
}
Message. setContent (multipart );
Message. saveChanges (); transport = s. getTransport ("smtp"); // smtp verification, that is, the user name and password of the email you use to send emails
Transport. connect (smtpServer, name, password); // send
Transport. sendMessage (message, message. getAllRecipients ());
Return true;
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
Transport. close ();
} Catch (MessagingException e ){
E. printStackTrace ();
}
}
Return false;
}
Public static void main (String [] args ){
BaseMail mail = new BaseMail ();
Mail. sendMail ("smtp.yeah.net", "12345", "password", "12345@yeah.net", "recipient's email address", "title", "content ", "file address ");}
}