怎样使用javamail的属性设置

原创 2004年07月21日 08:57:00

 

An SMTP protocol provider for the JavaMail API that provides access to an SMTP server. Refer to RFC 821 for more information.

The SMTP provider also supports ESMTP (RFC 1651). It can optionally use SMTP Authentication (RFC 2554) using the LOGIN, PLAIN, and DIGEST-MD5 mechanisms (RFC 2592 and RFC 2831).

To use SMTP authentication you'll need to provide the SMTP Transport with a username and password when connecting to the SMTP server. You can do this using one of the following approaches:

  • Provide an Authenticator object when creating your mail Session and provide the username and password information during the Authenticator callback.

    Note that the mail.smtp.user property can be set to provide a default username for the callback, but the password will still need to be supplied explicitly.

    This approach allows you to use the static Transport send method to send messages.

Call the Transport connect method explicitly with username and password arguments.

This approach requires you to explicitly manage a Transport object and use the Transport sendMessage method to send the message. The transport.java demo program demonstrates how to manage a Transport object. The following is roughly equivalent to the static Transport send method, but supplies the needed username and password:

 Transport tr = session.getTransport("smtp"); tr.connect(smtphost, username, password); msg.saveChanges(); // don't forget this tr.sendMessage(msg, msg.getAllRecipients()); tr.close(); 

You'll also need to supply an appropriate realm when using DIGEST-MD5 authentication; your mail server administrator can supply this information. You can set this using the mail.smtp.saslrealm property, or the setSASLRealm method on SMTPTransport.

SMTP can also optionally request Delivery Status Notifications (RFC 1891). The delivery status will typically be reported using a "multipart/report" (RFC 1892) message type with a "message/delivery-status" (RFC 1894) part. JavaMail does not currently provide direct support for these new MIME types, but you can process them as any other "multipart" or "message" content, using MimeMultipart and MimeMessage objects.

See below for the properties to enable these features.

Note also that THERE IS NOT SUFFICIENT DOCUMENTATION HERE TO USE THESE FEATURES!!! You will need to read the appropriate RFCs mentioned above to understand what these features do and how to use them. Don't just start setting properties and then complain to us when it doesn't work like you expect it to work. READ THE RFCs FIRST!!!

The SMTP protocol provider supports the following properties, which may be set in the JavaMail Session object. The properties are always set as strings; the Type column describes how the string is interpreted. For example, use

 props.put("mail.smtp.port", "888"); 
to set the mail.smtp.port property, which is of type int.

Name Type Description
mail.smtp.user String Default user name for SMTP.
mail.smtp.host String The SMTP server to connect to.
mail.smtp.port int The SMTP server port to connect to, if the connect() method doesn't explicitly specify one. Defaults to 25.
mail.smtp.connectiontimeout int Socket connection timeout value in milliseconds. Default is infinite timeout.
mail.smtp.timeout int Socket I/O timeout value in milliseconds. Default is infinite timeout.
mail.smtp.from String Email address to use for SMTP MAIL command. This sets the envelope return address. Defaults to msg.getFrom() or InternetAddress.getLocalAddress(). NOTE: mail.smtp.user was previously used for this.
mail.smtp.localhost String Local host name. Defaults to InetAddress.getLocalHost().getHostName(). Should not normally need to be set if your JDK and your name service are configured properly.
mail.smtp.ehlo boolean If false, do not attempt to sign on with the EHLO command. Defaults to true. Normally failure of the EHLO command will fallback to the HELO command; this property exists only for servers that don't fail EHLO properly or don't implement EHLO properly.
mail.smtp.auth boolean If true, attempt to authenticate the user using the AUTH command. Defaults to false.
mail.smtp.dsn.notify String The NOTIFY option to the RCPT command. Either NEVER, or some combination of SUCCESS, FAILURE, and DELAY (separated by commas).
mail.smtp.dsn.ret String The RET option to the MAIL command. Either FULL or HDRS.
mail.smtp.allow8bitmime boolean If set to true, and the server supports the 8BITMIME extension, text parts of messages that use the "quoted-printable" or "base64" encodings are converted to use "8bit" encoding if they follow the RFC2045 rules for 8bit text.
mail.smtp.sendpartial boolean If set to true, and a message has some valid and some invalid addresses, send the message anyway, reporting the partial failure with a SendFailedException. If set to false (the default), the message is not sent to any of the recipients if there is an invalid recipient address.
mail.smtp.saslrealm String The realm to use with DIGEST-MD5 authentication.
mail.smtp.quitwait boolean If set to true, causes the transport to wait for the response to the QUIT command. If set to false (the default), the QUIT command is sent and the connection is immediately closed. (NOTE: The default may change in the next release.)

In general, applications should not need to use the classes in this package directly. Instead, they should use the APIs defined by javax.mail package (and subpackages). Applications should never construct instances of SMTPTransport directly. Instead, they should use the Session method getTransport to acquire an appropriate Transport object.

JavaMail--基础类详解

 一、JavaMail简介 JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。我们可以基于Ja...
  • xufei512
  • xufei512
  • 2016年09月29日 16:53
  • 2227

Spring mail 邮件服务及其参数配置(properties文件的读写)

一个Web 系统通常会少不了邮件服务的,比如用于注册,密码找回,订单提醒等应用场景。Spring 封装了一个简单易用的关于邮件发送的工具类JavaMailSenderImpl 。系统要提供邮件服务,那...
  • houxuehan
  • houxuehan
  • 2016年08月11日 17:24
  • 8879

JavaMail通过IMAP和POP3接收未读以及设置已读邮件

[html] view plaincopy javamailpop3imap   使用javaMail收邮件主要有两种协议,一种是pop3,一种是imap。这两种协议都可以用来收邮件,但...
  • kent_kent_kent
  • kent_kent_kent
  • 2013年11月14日 18:57
  • 8145

使用javamail.jar进行邮件发送包含代理设置

下面给大家贡献一份使用javamail进行邮件发送的例子,其中包含有代理设置的功能,可以通过代理进行邮件发送。 import java.util.ArrayList; import java.util...
  • wyc_cs
  • wyc_cs
  • 2013年05月09日 09:17
  • 1953

Eclispe 快捷键使用说明+相关属性设置

  • 2011年08月24日 10:46
  • 285KB
  • 下载

gridctrl控件的使用示例程序,程序中有关于gridctrl控件的属性设置、各种方法的使用

  • 2011年03月18日 21:16
  • 391KB
  • 下载

JavaMail通过IMAP和POP3接收未读以及设置已读邮件

javamailpop3imap 使用javaMail收邮件主要有两种协议,一种是pop3,一种是imap。这两种协议都可以用来收邮件,但是在其中的处理上是有区别的。pop3是不支持判断邮件是否为已读...
  • u011382076
  • u011382076
  • 2013年10月16日 17:10
  • 2709

JavaMail通过IMAP和POP3接收未读以及设置已读邮件

[html] view plaincopy javamailpop3imap   使用javaMail收邮件主要有两种协议,一种是pop3,一种是imap。这两种协议都可以用来收邮件,但...
  • kent_kent_kent
  • kent_kent_kent
  • 2013年11月14日 18:57
  • 8145

JavaMail设置重要度Header等信息

JavaMail中设置Header的方法: public void setHeader(java.lang.String name,                       java.la...
  • hantiannan
  • hantiannan
  • 2012年04月13日 17:21
  • 5815

JavaMail通过IMAP和POP3接收未读以及设置已读邮件

使用javaMail收邮件主要有两种协议,一种是pop3,一种是imap。这两种协议都可以用来收邮件,但是在其中的处理上是有区别的。pop3是不支持判断邮件是否为已读的,也就是说你不能直接从收件箱里面...
  • u012460952
  • u012460952
  • 2014年03月06日 11:10
  • 2106
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:怎样使用javamail的属性设置
举报原因:
原因补充:

(最多只允许输入30个字)