本文整理了Java中javax.mail.Message
类的一些代码示例,展示了Message
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message
类的具体详情如下:
包路径:javax.mail.Message
类名称:Message
[英]This class models an email message. This is an abstract class. Subclasses provide actual implementations.
Message implements the Part interface. Message contains a set of attributes and a "content". Messages within a folder also have a set of flags that describe its state within the folder.
Message defines some new attributes in addition to those defined in the Part
interface. These attributes specify meta-data for the message - i.e., addressing and descriptive information about the message.
Message objects are obtained either from a Folder or by constructing a new Message object of the appropriate subclass. Messages that have been received are normally retrieved from a folder named "INBOX".
A Message object obtained from a folder is just a lightweight reference to the actual message. The Message is 'lazily' filled up (on demand) when each item is requested from the message. Note that certain folder implementations may return Message objects that are pre-filled with certain user-specified items. To send a message, an appropriate subclass of Message (e.g., MimeMessage) is instantiated, the attributes and content are filled in, and the message is sent using the Transport.send
method.
[中]这个类为电子邮件建模。这是一个抽象类。子类提供实际的实现。
消息实现部件接口。消息包含一组属性和一个“内容”。文件夹中的邮件还具有一组标志,用于描述其在文件夹中的状态。
除了Part
接口中定义的属性外,Message还定义了一些新属性。这些属性指定消息的元数据,即消息的地址和描述性信息。
消息对象可以从文件夹中获取,也可以通过构造适当子类的新消息对象来获取。收到的邮件通常从名为“收件箱”的文件夹中检索。
从文件夹中获取的消息对象只是对实际消息的轻量级引用。当从消息中请求每个项目时,消息被“延迟”填充(按需)。请注意,某些文件夹实现可能会返回预先填充了某些用户指定项的消息对象。要发送消息,将实例化消息的适当子类(例如,mimessage),填写属性和内容,并使用Transport.send
方法发送消息。
代码示例来源:origin: stackoverflow.com
final String password = "yourpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
message.setFrom(new InternetAddress("your_user_name@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to_email_address@domain.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
代码示例来源:origin: pentaho/pentaho-kettle
msg.setHeader( "X-Priority", priority_int ); // (String)int between 1= high and 3 = low.
msg.setHeader( "Importance", meta.getImportance() );
msg.setHeader( "Sensitivity", meta.getSensitivity() );
msg.setFrom( new InternetAddress( email_address ) );
} else {
throw new MessagingException( BaseMessages.getString( PKG, "Mail.Error.ReplyEmailNotFilled" ) );
msg.setReplyTo( address );
msg.setRecipients( Message.RecipientType.TO, address );
msg.setRecipients( Message.RecipientType.CC, addressCc );
msg.setRecipients( Message.RecipientType.BCC, addressBCc );
msg.setSubject( mailsubject );
msg.setSentDate( new Date() );
StringBuilder messageText = new StringBuilder();
msg.setContent( data.parts );
transport.sendMessage( msg, msg.getAllRecipients() );
} finally {
if ( transport != null ) {
代码示例来源:origin: pentaho/pentaho-kettle
r[index] = new Long( message.getMessageNumber() );
break;
case MailInputField.COLUMN_SUBJECT:
r[index] = message.getSubject();
break;
case MailInputField.COLUMN_SENDER:
r[index] = StringUtils.join( message.getFrom(), ";" );
break;
case MailInputField.COLUMN_REPLY_TO:
r[index] = StringUtils.join( message.getReplyTo(), ";" );
break;
case MailInputField.COLUMN_RECIPIENTS:
r[index] = StringUtils.join( message.getAllRecipients(), ";" );
break;
case MailInputField.COLUMN_DESCRIPTION:
r[index] = message.getDescription();
break;
case MailInputField.COLUMN_BODY:
break;
case MailInputField.COLUMN_RECEIVED_DATE:
Date receivedDate = message.getReceivedDate();
r[index] = receivedDate != null ? new Date( receivedDate.getTime() ) : null;
break;
case MailInputField.COLUMN_SENT_DATE:
Date sentDate = message.getSentDate();
r[index] = sentDate != null ? new Date( sentDate.getTime() ) : null;
break;
case MailInputField.COLUMN_CONTENT_TYPE:
代码示例来源:origin: stackoverflow.com
import javax.mail.*;
import javax.mail.internet.*;
// Set up the SMTP server.
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "smtp.myisp.com");
Session session = Session.getDefaultInstance(props, null);
// Construct the message
String to = "you@you.com";
String from = "me@me.com";
String subject = "Hello";
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Hi,\n\nHow are you?");
// Send the message.
Transport.send(msg);
} catch (MessagingException e) {
// Error.
}
代码示例来源:origin: aa112901/remusic
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", 25, "remusic_log@163.com",
"remusiclog1");
Message mailMessage = new SMTPMessage(session);
Address from = new InternetAddress("remusic_log@163.com");
mailMessage.setFrom(from);
Address to = new InternetAddress("remusic_log@163.com");
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(title);
mailMessage.setSentDate(new Date());
mailMessage.setText(content);
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
return true;
ex.printStackTrace();
代码示例来源:origin: pentaho/pentaho-kettle
Properties props = new Properties();
props.put( "mail.smtp.host", ArgList[0] );
Session session = Session.getDefaultInstance( props, null );
session.setDebug( debug );
Message msg = new MimeMessage( session );
InternetAddress addressFrom = new InternetAddress( (String) ArgList[1] );
msg.setFrom( addressFrom );
addressTo[i] = new InternetAddress( strArrRecipients[i] );
msg.setRecipients( Message.RecipientType.TO, addressTo );
msg.addHeader( "MyHeaderName", "myHeaderValue" );
msg.setSubject( (String) ArgList[3] );
msg.setContent( ArgList[4], "text/plain" );
Transport.send( msg );
} catch ( Exception e ) {
throw new RuntimeException( "sendMail: " + e.toString() );
代码示例来源:origin: JpressProjects/jpress
private Message createMessage() {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", "25");
if (useSSL) {
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.port", "465");
}
// error:javax.mail.MessagingException: 501 Syntax: HELO hostname
props.setProperty("mail.smtp.localhost", "127.0.0.1");
Session session = Session.getInstance(props, this);
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(MimeUtility.encodeText(name) + "<" + name + ">"));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return message;
}
代码示例来源:origin: javaee-samples/javaee7-samples
out.println("<h1>Sending email using JavaMail API</h1>");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.debug", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
+ "\"...<br>");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(creds.getFrom()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(creds.getTo()));
message.setSubject("Sending message using Programmatic JavaMail " + Calendar.getInstance().getTime());
message.setText("Java EE 7 is cool!");
Transport.send(message, message.getAllRecipients());
代码示例来源:origin: stackoverflow.com
Properties properties = System.getProperties();
properties.put("mail.smtp.host", server);
properties.put("mail.smtp.port", "" + port);
Session session = Session.getInstance(properties);
Transport transport = session.getTransport("smtp");
transport.connect(server, username, password);
for (int i = 0; i < count; i++) {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject + "JavaMail API");
message.setSentDate(new Date());
setHTMLContent(message);
message.saveChanges();
transport.sendMessage(message, address);
}
transport.close();
代码示例来源:origin: azkaban/azkaban
public void sendEmail() throws MessagingException {
checkSettings();
final Properties props = new Properties();
if (this._usesAuth) {
props.put("mail.smtp.auth", "true");
props.put("mail.user", this._mailUser);
props.put("mail.password", this._mailPassword);
} else {
final Message message = sender.createMessage();
final InternetAddress from = new InternetAddress(this._fromAddress, false);
message.setFrom(from);
for (final String toAddr : this._toAddress) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
toAddr, false));
message.setSubject(this._subject);
message.setSentDate(new Date());
message.setContent(multipart);
} else {
message.setContent(this._body.toString(), this._mimeType);
代码示例来源:origin: bluejoe2008/openwebflow
public void sendMail(String receiver, String subject, String message) throws Exception
{
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");//发送邮件协议
properties.setProperty("mail.smtp.auth", "true");//需要验证
Session session = Session.getInstance(properties);
session.setDebug(false);
//邮件信息
Message messgae = new MimeMessage(session);
messgae.setFrom(new InternetAddress(getMailFrom()));//设置发送人
messgae.setText(message);//设置邮件内容
messgae.setSubject(subject);//设置邮件主题
//发送邮件
Transport tran = session.getTransport();
tran.connect(getServerHost(), getServerPort(), getAuthUserName(), getAuthPassword());
tran.sendMessage(messgae, new Address[] { new InternetAddress(receiver) });//设置邮件接收人
tran.close();
Logger.getLogger(this.getClass()).debug(String.format("sent mail to <%s>: %s", receiver, subject));
}
}
代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components
public enum MailAuthType {
SSL,
TLS,
NONE
}
代码示例来源:origin: xpmatteo/birthday-greetings-kata
private void sendMessage(String smtpHost, int smtpPort, String sender, String subject, String body, String recipient) throws AddressException, MessagingException {
// Create a mail session
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "" + smtpPort);
Session session = Session.getInstance(props, null);
// Construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(sender));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
msg.setSubject(subject);
msg.setText(body);
// Send the message
Transport.send(msg);
}
}
代码示例来源:origin: opensourceBIM/BIMserver
public void send() throws MessagingException, UserException {
Properties props = new Properties();
ServerSettings serverSettings = bimServer.getServerSettingsCache().getServerSettings();
props.put("mail.smtp.localhost", "bimserver.org");
String smtpProps = serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS ? "mail.smtps.port" : "mail.smtp.port";
props.put("mail.smtp.connectiontimeout", 10000);
props.put("mail.smtp.timeout", 10000);
props.put("mail.smtp.writetimeout", 10000);
Session mailSession = Session.getInstance(props);
throw new RuntimeException("Unimplemented SMTP protocol: " + serverSettings.getSmtpProtocol());
transport.connect(serverSettings.getSmtpServer(), serverSettings.getSmtpPort(), serverSettings.getSmtpUsername(), serverSettings.getSmtpPassword());
Message message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setRecipients(to, addressTo);
message.setContent(body, contentType);
message.setFrom(from);
transport.sendMessage(message, addressTo);
} catch (MessagingException e) {
LOGGER.error("Error sending email " + body + " " + e.getMessage());
throw new UserException("Error sending email " + e.getMessage());
代码示例来源:origin: stackoverflow.com
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
Transport t = session.getTransport("smtp");
t.connect(userName, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
代码示例来源:origin: stackoverflow.com
Message mesg;
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", SMTP_PORT1);
props.put("mail.smtp.host", SMTP_HOST1);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
session.setDebug(true);
mesg.setFrom(new InternetAddress(IMAP_ACCOUNT1));
InternetAddress toAddress = new InternetAddress(message_dest);
mesg.addRecipient(Message.RecipientType.TO, toAddress);
mesg.setSubject(message_objet);
mesg.setText(message_corps);
Transport t = session.getTransport("smtp");
try {
t.connect("xx@xx.com", "mypwd");
t.sendMessage(mesg, mesg.getAllRecipients());
} finally {
t.close();
代码示例来源:origin: stackoverflow.com
final String password = "************";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
Transport transport=session.getTransport();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemail@gmail.com"));//formBean.getString("fromEmail")
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("myemail@gmail.com"));
message.setSubject("subject");//formBean.getString(
message.setText("mailBody");
transport.connect();
transport.send(message, InternetAddress.parse("myemail@gmail.com"));//(message);
e.printStackTrace();
throw new RuntimeException(e);
代码示例来源:origin: stackoverflow.com
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendMail {
public static sendMail(String from, String to, String subject, String body)
throws MessagingException
{
final Properties p = new Properties();
p.put("mail.smtp.host", "localhost");
final Message msg = new MimeMessage(Session.getDefaultInstance(p));
msg.setFrom(new InternetAddress(from));
msg.addRecipient(RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
}
}
代码示例来源:origin: stackoverflow.com
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(GOOGLE_USERNAME, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(FROM_EMAIL, FROM_NAME));
// rest of the email settings
代码示例来源:origin: stackoverflow.com
public void sendEmail(){
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "false");
Session session = Session.getInstance(props);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
final String emptyPassword = null;
transport.connect("smtp.gmail.com", 587, "username@gmail.com", emptyPassword);
// you can get the access token when you do Auth2 for gmail, I haven't tried this with just a password
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", "username@gmail.com", accessToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);
Message message = new MimeMessage(session);
//add needed stuff to the message before sending it.
transport.sendMessage(message, message.getAllRecipients());
transport.close();
内容来源于网络,如有侵权,请联系作者删除!