首页

javamail1 发送Email例子(含详细的使用文档)

java

2020-7-30

/*
 * Copyright (c) 1996-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

/**
 * sendfile will create a multipart message with the second
 * block of the message being the given file.<p>
 *
 * This demonstrates how to use the FileDataSource to send
 * a file via mail.<p>
 *
 * usage: <code>java sendfile <i>to from smtp file true|false</i></code>
 * where <i>to</i> and <i>from</i> are the destination and
 * origin email addresses, respectively, and <i>smtp</i>
 * is the hostname of the machine that has smtp server
 * running.  <i>file</i> is the file to send. The next parameter
 * either turns on or turns off debugging during sending.
 *
 * @author	Christopher Cotton
 */
public class sendfile {

    public static void main(String[] args) {
	if (args.length != 5) {
	    System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
	    System.exit(1);
	}

	String to = args[0];
	String from = args[1];
	String host = args[2];
	String filename = args[3];
	boolean debug = Boolean.valueOf(args[4]).booleanValue();
	String msgText1 = "Sending a file.\n";
	String subject = "Sending a file";
	
	// create some properties and get the default Session
	Properties props = System.getProperties();
	props.put("mail.smtp.host", host);
	
	Session session = Session.getInstance(props, null);
	session.setDebug(debug);
	
	try {
	    // create a message
	    MimeMessage msg = new MimeMessage(session);
	    msg.setFrom(new InternetAddress(from));
	    InternetAddress[] address = {new InternetAddress(to)};
	    msg.setRecipients(Message.RecipientType.TO, address);
	    msg.setSubject(subject);

	    // create and fill the first message part
	    MimeBodyPart mbp1 = new MimeBodyPart();
	    mbp1.setText(msgText1);

	    // create the second message part
	    MimeBodyPart mbp2 = new MimeBodyPart();

	    // attach the file to the message
	    mbp2.attachFile(filename);

	    /*
	     * Use the following approach instead of the above line if
	     * you want to control the MIME type of the attached file.
	     * Normally you should never need to do this.
	     *
	    FileDataSource fds = new FileDataSource(filename) {
		public String getContentType() {
		    return "application/octet-stream";
		}
	    };
	    mbp2.setDataHandler(new DataHandler(fds));
	    mbp2.setFileName(fds.getName());
	     */

	    // create the Multipart and add its parts to it
	    Multipart mp = new MimeMultipart();
	    mp.addBodyPart(mbp1);
	    mp.addBodyPart(mbp2);

	    // add the Multipart to the message
	    msg.setContent(mp);

	    // set the Date: header
	    msg.setSentDate(new Date());

	    /*
	     * If you want to control the Content-Transfer-Encoding
	     * of the attached file, do the following.  Normally you
	     * should never need to do this.
	     *
	    msg.saveChanges();
	    mbp2.setHeader("Content-Transfer-Encoding", "base64");
	     */

	    // send the message
	    Transport.send(msg);
	    
	} catch (MessagingException mex) {
	    mex.printStackTrace();
	    Exception ex = null;
	    if ((ex = mex.getNextException()) != null) {
		ex.printStackTrace();
	    }
	} catch (IOException ioex) {
	    ioex.printStackTrace();
	}
    }
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
javamail-1.4.7/mail.jar , 521000
javamail-1.4.7/lib/imap.jar , 181492
javamail-1.4.7/lib/smtp.jar , 52869
javamail-1.4.7/lib/pop3.jar , 44405
javamail-1.4.7/lib/dsn.jar , 19171
javamail-1.4.7/lib/gimap.jar , 14769
javamail-1.4.7/lib/mailapi.jar , 279893
javamail-1.4.7/demo/folderlist.java , 5812
javamail-1.4.7/demo/README.txt , 11402
javamail-1.4.7/demo/monitor.java , 4075
javamail-1.4.7/demo/registry.java , 6895
javamail-1.4.7/demo/msgmultisendsample.java , 3996
javamail-1.4.7/demo/smtpsend.java , 11245
javamail-1.4.7/demo/search.java , 8682
javamail-1.4.7/demo/msgsendsample.java , 4629
javamail-1.4.7/demo/sendhtml.java , 7013
javamail-1.4.7/demo/sendfile.java , 4820
javamail-1.4.7/demo/msgshow.java , 12609
javamail-1.4.7/demo/namespace.java , 4855
javamail-1.4.7/demo/CRLFOutputStream.java , 3004
javamail-1.4.7/demo/mover.java , 5693
javamail-1.4.7/demo/msgsend.java , 7338
javamail-1.4.7/demo/uidmsgshow.java , 8865
javamail-1.4.7/demo/transport.java , 8154
javamail-1.4.7/demo/NewlineOutputStream.java , 2874
javamail-1.4.7/demo/populate.java , 7445
javamail-1.4.7/demo/copier.java , 3760
javamail-1.4.7/demo/client/FolderModel.java , 4217
javamail-1.4.7/demo/client/StoreTreeNode.java , 3815
javamail-1.4.7/demo/client/FolderViewer.java , 3245
javamail-1.4.7/demo/client/SimpleClient.java , 5344
javamail-1.4.7/demo/client/README.txt , 4069
javamail-1.4.7/demo/client/MultipartViewer.java , 5255
javamail-1.4.7/demo/client/simple.mailcap , 196
javamail-1.4.7/demo/client/FolderTreeNode.java , 3732
javamail-1.4.7/demo/client/TextViewer.java , 3232
javamail-1.4.7/demo/client/MessageViewer.java , 6926
javamail-1.4.7/demo/client/ComponentFrame.java , 2586
javamail-1.4.7/demo/client/SimpleAuthenticator.java , 4673
javamail-1.4.7/demo/servlet/JavaMailServlet.java , 21082
javamail-1.4.7/demo/servlet/JavaMail.html , 4601
javamail-1.4.7/demo/servlet/README.txt , 6361
javamail-1.4.7/demo/webapp/src/classes/demo/MailUserBean.java , 5106
javamail-1.4.7/demo/webapp/src/classes/demo/FilterServlet.java , 3141
javamail-1.4.7/demo/webapp/src/classes/demo/AttachmentServlet.java , 3477
javamail-1.4.7/demo/webapp/src/docroot/WEB-INF/web.xml , 3503
javamail-1.4.7/demo/webapp/src/docroot/errordetails.jsp , 1783
javamail-1.4.7/demo/webapp/src/docroot/messagecontent.jsp , 3299
javamail-1.4.7/demo/webapp/src/docroot/login.jsp , 2145
javamail-1.4.7/demo/webapp/src/docroot/folders.jsp , 2445
javamail-1.4.7/demo/webapp/src/docroot/index.html , 4367
javamail-1.4.7/demo/webapp/src/docroot/send.jsp , 2151
javamail-1.4.7/demo/webapp/src/docroot/messageheaders.jsp , 3255
javamail-1.4.7/demo/webapp/src/docroot/compose.jsp , 3194
javamail-1.4.7/demo/webapp/src/docroot/logout.jsp , 1954
javamail-1.4.7/demo/webapp/src/docroot/errorpage.jsp , 1992
javamail-1.4.7/demo/webapp/build.sh , 2101
javamail-1.4.7/demo/webapp/webapp.README.txt , 4856
javamail-1.4.7/demo/webapp/build.bat , 2291
javamail-1.4.7/demo/webapp/src/taglib/demo/SendTag.java , 4037
javamail-1.4.7/demo/webapp/src/taglib/demo/MessageInfo.java , 8113
javamail-1.4.7/demo/webapp/src/taglib/demo/ListMessagesTag.java , 4054
javamail-1.4.7/demo/webapp/src/taglib/demo/AttachmentInfo.java , 4006
javamail-1.4.7/demo/webapp/src/taglib/demo/MessageTag.java , 3164
javamail-1.4.7/demo/webapp/src/taglib/demo/ListAttachmentsTag.java , 3772
javamail-1.4.7/demo/webapp/src/taglib/demo/ListAttachmentsTEI.java , 2193
javamail-1.4.7/demo/webapp/src/taglib/demo/MessageTEI.java , 2147
javamail-1.4.7/demo/webapp/src/taglib/demo/ListMessagesTEI.java , 2182
javamail-1.4.7/demo/webapp/src/taglib/META-INF/taglib.tld , 4881
javamail-1.4.7/demo/logging/SummaryFormatter.java , 3369
javamail-1.4.7/demo/logging/FileErrorManager.java , 9156
javamail-1.4.7/demo/logging/SummaryNameFormatter.java , 4343
javamail-1.4.7/demo/logging/MailHandlerDemo.java , 15541
javamail-1.4.7/demo/logging/README.txt , 1942
javamail-1.4.7/demo/logging/maildemo.properties , 1415
javamail-1.4.7/demo/outlook/MSMultipartDataSource.java , 4374
javamail-1.4.7/demo/outlook/README.txt , 362
javamail-1.4.7/demo/outlook/MSBodyPart.java , 3903
javamail-1.4.7/demo/outlook/MSMessage.java , 7173
javamail-1.4.7/SSLNOTES.txt , 11663
javamail-1.4.7/README.txt , 8530
javamail-1.4.7/LICENSE.txt , 16181
javamail-1.4.7/NOTES.txt , 9513
javamail-1.4.7/NTLMNOTES.txt , 1200
javamail-1.4.7/distributionREADME.txt , 1726
javamail-1.4.7/COPYRIGHT.txt , 3322
javamail-1.4.7/CHANGES.txt , 29117
javamail-1.4.7/COMPAT.txt , 5899
javamail-1.4.7/docs/JavaMail-1.4-changes.txt , 38322
javamail-1.4.7/docs/JavaMail-1.2-changes.txt , 41785
javamail-1.4.7/docs/JavaMail-1.3-changes.txt , 12193
javamail-1.4.7/docs/JavaMail-1.1-changes.txt , 23606
javamail-1.4.7/docs/javadocs/overview-summary.html , 15172
javamail-1.4.7/docs/javadocs/serialized-form.html , 47530
javamail-1.4.7/docs/javadocs/allclasses-frame.html , 19139
javamail-1.4.7/docs/javadocs/resources/tab.gif , 291
javamail-1.4.7/docs/javadocs/resources/background.gif , 2313
javamail-1.4.7/docs/javadocs/resources/titlebar.gif , 10701
javamail-1.4.7/docs/javadocs/resources/titlebar_end.gif , 849
javamail-1.4.7/docs/javadocs/constant-values.html , 25902
javamail-1.4.7/docs/javadocs/stylesheet.css , 11139
javamail-1.4.7/docs/javadocs/index-files/index-23.html , 9735
javamail-1.4.7/docs/javadocs/index-files/index-12.html , 11545
javamail-1.4.7/docs/javadocs/index-files/index-16.html , 23882
javamail-1.4.7/docs/javadocs/index-files/index-9.html , 32969
javamail-1.4.7/docs/javadocs/index-files/index-5.html , 23190
javamail-1.4.7/docs/javadocs/index-files/index-1.html , 37826
javamail-1.4.7/docs/javadocs/index-files/index-4.html , 20195
javamail-1.4.7/docs/javadocs/index-files/index-22.html , 7058
javamail-1.4.7/docs/javadocs/index-files/index-13.html , 54235
javamail-1.4.7/docs/javadocs/index-files/index-8.html , 20537
javamail-1.4.7/docs/javadocs/index-files/index-17.html , 8407
javamail-1.4.7/docs/javadocs/index-files/index-6.html , 27084
javamail-1.4.7/docs/javadocs/index-files/index-19.html , 105509
javamail-1.4.7/docs/javadocs/index-files/index-2.html , 9494
javamail-1.4.7/docs/javadocs/index-files/index-20.html , 22369
javamail-1.4.7/docs/javadocs/index-files/index-11.html , 6137
javamail-1.4.7/docs/javadocs/index-files/index-15.html , 8391
javamail-1.4.7/docs/javadocs/index-files/index-24.html , 5663
javamail-1.4.7/docs/javadocs/index-files/index-21.html , 12058
javamail-1.4.7/docs/javadocs/index-files/index-10.html , 6714
javamail-1.4.7/docs/javadocs/index-files/index-14.html , 23354
javamail-1.4.7/docs/javadocs/index-files/index-18.html , 34066
javamail-1.4.7/docs/javadocs/index-files/index-7.html , 163768
javamail-1.4.7/docs/javadocs/index-files/index-3.html , 35642
javamail-1.4.7/docs/javadocs/com/sun/mail/util/MailSSLSocketFactory.html , 41298
javamail-1.4.7/docs/javadocs/com/sun/mail/util/ReadableMime.html , 8185
javamail-1.4.7/docs/javadocs/com/sun/mail/util/class-use/ReadableMime.html , 9075
javamail-1.4.7/docs/javadocs/com/sun/mail/util/class-use/MailSSLSocketFactory.html , 4417
javamail-1.4.7/docs/javadocs/com/sun/mail/util/logging/package-tree.html , 5069
javamail-1.4.7/docs/javadocs/com/sun/mail/util/logging/MailHandler.html , 78108
javamail-1.4.7/docs/javadocs/com/sun/mail/util/logging/class-use/MailHandler.html , 4451
javamail-1.4.7/docs/javadocs/com/sun/mail/util/logging/package-use.html , 4091
javamail-1.4.7/docs/javadocs/com/sun/mail/util/logging/package-summary.html , 5859
javamail-1.4.7/docs/javadocs/com/sun/mail/util/logging/package-frame.html , 880
javamail-1.4.7/docs/javadocs/com/sun/mail/util/package-use.html , 7539
javamail-1.4.7/docs/javadocs/com/sun/mail/util/package-tree.html , 5485
javamail-1.4.7/docs/javadocs/com/sun/mail/util/package-summary.html , 6916
javamail-1.4.7/docs/javadocs/com/sun/mail/util/package-frame.html , 1049
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/POP3Folder.html , 59549
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/package-frame.html , 1159
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/POP3Message.html , 65773
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/class-use/POP3SSLStore.html , 4329
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/class-use/POP3Store.html , 6185
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/class-use/POP3Message.html , 6443
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/class-use/POP3Folder.html , 4307
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/POP3Store.html , 32839
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/package-tree.html , 6482
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/package-use.html , 5602
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/package-summary.html , 24116
javamail-1.4.7/docs/javadocs/com/sun/mail/pop3/POP3SSLStore.html , 15105
最多只能显示150条信息!
没有账号? 忘记密码?

社交账号快速登录