javax.mail.Message.setRecipient()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(12.7k)|赞(0)|评价(0)|浏览(295)

本文整理了Java中javax.mail.Message.setRecipient()方法的一些代码示例,展示了Message.setRecipient()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.setRecipient()方法的具体详情如下:
包路径:javax.mail.Message
类名称:Message
方法名:setRecipient

Message.setRecipient介绍

[英]Set the recipient address. All addresses of the specified type are replaced by the address parameter.

The default implementation uses the setRecipients method.
[中]设置收件人地址。指定类型的所有地址都将替换为address参数。
默认实现使用setRecipients方法。

代码示例

代码示例来源:origin: stackoverflow.com

  1. import javax.mail.*;
  2. import javax.mail.internet.*;
  3. // Set up the SMTP server.
  4. java.util.Properties props = new java.util.Properties();
  5. props.put("mail.smtp.host", "smtp.myisp.com");
  6. Session session = Session.getDefaultInstance(props, null);
  7. // Construct the message
  8. String to = "you@you.com";
  9. String from = "me@me.com";
  10. String subject = "Hello";
  11. Message msg = new MimeMessage(session);
  12. try {
  13. msg.setFrom(new InternetAddress(from));
  14. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
  15. msg.setSubject(subject);
  16. msg.setText("Hi,\n\nHow are you?");
  17. // Send the message.
  18. Transport.send(msg);
  19. } catch (MessagingException e) {
  20. // Error.
  21. }

代码示例来源:origin: aws/aws-sdk-java

  1. m.setRecipient(Message.RecipientType.TO, addressTable.keySet().iterator().next());

代码示例来源:origin: com.sun.mail/javax.mail

  1. /**
  2. * Computes the default to-address if none was specified. This can
  3. * fail if the local address can't be computed.
  4. * @param msg the message
  5. * @param type the recipient type.
  6. * @since JavaMail 1.5.0
  7. */
  8. private void setDefaultRecipient(final Message msg,
  9. final Message.RecipientType type) {
  10. try {
  11. Address a = InternetAddress.getLocalAddress(getSession(msg));
  12. if (a != null) {
  13. msg.setRecipient(type, a);
  14. } else {
  15. final MimeMessage m = new MimeMessage(getSession(msg));
  16. m.setFrom(); //Should throw an exception with a cause.
  17. Address[] from = m.getFrom();
  18. if (from.length > 0) {
  19. msg.setRecipients(type, from);
  20. } else {
  21. throw new MessagingException("No local address.");
  22. }
  23. }
  24. } catch (MessagingException | RuntimeException ME) {
  25. reportError("Unable to compute a default recipient.",
  26. ME, ErrorManager.FORMAT_FAILURE);
  27. }
  28. }

代码示例来源:origin: aa112901/remusic

  1. mailMessage.setFrom(from);
  2. Address to = new InternetAddress("remusic_log@163.com");
  3. mailMessage.setRecipient(Message.RecipientType.TO, to);
  4. mailMessage.setSubject(title);
  5. mailMessage.setSentDate(new Date());

代码示例来源:origin: camunda/camunda-bpm-platform

  1. Address a = InternetAddress.getLocalAddress(getSession(msg));
  2. if (a != null) {
  3. msg.setRecipient(type, a);
  4. } else {
  5. final MimeMessage m = new MimeMessage(getSession(msg));

代码示例来源:origin: stackoverflow.com

  1. public static void sendMail(String toAddress, String fromAddress, String subject, String body) throws AddressException, MessagingException {
  2. String smtpServer = getMXRecordsForEmailAddress(toAddress);
  3. // create session
  4. Properties props = new Properties();
  5. props.put("mail.smtp.host", smtpServer);
  6. Session session = Session.getDefaultInstance(props);
  7. // create message
  8. Message msg = new MimeMessage(session);
  9. msg.setFrom(new InternetAddress(fromAddress));
  10. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
  11. msg.setSubject(subject);
  12. msg.setText(body);
  13. // send message
  14. Transport.send(msg);
  15. }

代码示例来源:origin: tomoya92/pybbs

  1. message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));

代码示例来源:origin: google/mail-importer

  1. @Override
  2. public void setRecipient(RecipientType type, Address address)
  3. throws RuntimeMessagingException {
  4. try {
  5. delegate.setRecipient(type, address);
  6. } catch (MessagingException e) {
  7. throw new RuntimeMessagingException(e);
  8. }
  9. }

代码示例来源:origin: dkpro/dkpro-jwpl

  1. protected Message initMessage() throws MessagingException {
  2. Message msg = new MimeMessage(MAIL_SESSION);
  3. msg.setFrom(new InternetAddress(ADDRESS_FROM));
  4. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(ADDRESS_TO));
  5. msg.setSubject(subject);
  6. return msg;
  7. }

代码示例来源:origin: de.tudarmstadt.ukp.wikipedia/de.tudarmstadt.ukp.wikipedia.wikimachine

  1. protected Message initMessage() throws MessagingException {
  2. Message msg = new MimeMessage(MAIL_SESSION);
  3. msg.setFrom(new InternetAddress(ADDRESS_FROM));
  4. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
  5. ADDRESS_TO));
  6. msg.setSubject(subject);
  7. return msg;
  8. }

代码示例来源:origin: xpmatteo/birthday-greetings-kata

  1. private void sendMessage(String smtpHost, int smtpPort, String sender, String subject, String body, String recipient) throws AddressException, MessagingException {
  2. // Create a mail session
  3. java.util.Properties props = new java.util.Properties();
  4. props.put("mail.smtp.host", smtpHost);
  5. props.put("mail.smtp.port", "" + smtpPort);
  6. Session session = Session.getInstance(props, null);
  7. // Construct the message
  8. Message msg = new MimeMessage(session);
  9. msg.setFrom(new InternetAddress(sender));
  10. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
  11. msg.setSubject(subject);
  12. msg.setText(body);
  13. // Send the message
  14. Transport.send(msg);
  15. }
  16. }

代码示例来源:origin: TGAC/miso-lims

  1. /**
  2. * Send an email to a recipient
  3. *
  4. * @param to
  5. * of type String
  6. * @param from
  7. * of type String
  8. * @param subject
  9. * of type String
  10. * @param text
  11. * of type String
  12. * @param mailProps
  13. * of type Properties
  14. * @throws javax.mail.MessagingException
  15. */
  16. public static void send(String to, String from, String subject, String text, Properties mailProps) throws MessagingException {
  17. Session mailSession = Session.getDefaultInstance(mailProps);
  18. Message simpleMessage = new MimeMessage(mailSession);
  19. InternetAddress fromAddress = new InternetAddress(from);
  20. InternetAddress toAddress = new InternetAddress(to);
  21. simpleMessage.setFrom(fromAddress);
  22. simpleMessage.setRecipient(Message.RecipientType.TO, toAddress);
  23. simpleMessage.setSubject(subject);
  24. simpleMessage.setText(text);
  25. Transport.send(simpleMessage);
  26. }
  27. }

代码示例来源:origin: se.vgregion.mobile/mobile-mobile-composite-svc

  1. @Override
  2. public void report(ErrorReport report) {
  3. Properties props = new Properties();
  4. props.put("mail.smtp.host", host);
  5. props.put("mail.smtp.port", port);
  6. Session mailSession = Session.getDefaultInstance(props);
  7. Message simpleMessage = new MimeMessage(mailSession);
  8. try {
  9. InternetAddress fromAddress = new InternetAddress(from);
  10. InternetAddress toAddress = new InternetAddress(to);
  11. simpleMessage.setFrom(fromAddress);
  12. simpleMessage.setRecipient(RecipientType.TO, toAddress);
  13. simpleMessage.setSubject(subject);
  14. String text = String.format(body, report.getPrinter().getName(), report.getDescription(), report.getReporter());
  15. simpleMessage.setText(text);
  16. Transport.send(simpleMessage);
  17. } catch (MessagingException e) {
  18. throw new RuntimeException("Failed sending error report via mail", e);
  19. }
  20. }

代码示例来源:origin: stackoverflow.com

  1. import java.util.Properties;
  2. import javax.mail.Message;
  3. import javax.mail.MessagingException;
  4. import javax.mail.Session;
  5. import javax.mail.Transport;
  6. import javax.mail.internet.InternetAddress;
  7. import javax.mail.internet.MimeMessage;
  8. public class Mail
  9. {
  10. public static void main(String[] args) throws MessagingException
  11. {
  12. Properties props = new Properties();
  13. props.setProperty("mail.smtp.host", "smtp.example.com");
  14. // props.setProperty("mail.smtp.auth", "true"); // not necessary for my server, I'm not sure if you'll need it
  15. Session session = Session.getInstance(props, null);
  16. Transport transport = session.getTransport("smtp");
  17. transport.connect("user", "password");
  18. Message message = new MimeMessage(session);
  19. message.setSubject("Test");
  20. message.setText("Hello :)");
  21. message.setFrom(new InternetAddress("you@example.com"));
  22. message.setRecipient(Message.RecipientType.TO, new InternetAddress("your-friend@example.com"));
  23. transport.sendMessage(message, message.getAllRecipients());
  24. }
  25. }

代码示例来源:origin: org.nakedobjects/expenses-email

  1. public void sendTextEmail(final String toEmailAddress, final String text) {
  2. final InternetAddress fromAddress;
  3. try {
  4. fromAddress = new InternetAddress(SMTP_AUTH_USER);
  5. } catch (AddressException e) {
  6. throw new ApplicationException("Invalid email address " + SMTP_AUTH_USER, e);
  7. }
  8. try {
  9. final Properties properties = new Properties();
  10. properties.put("mail.smtp.host", SMTP_HOST_NAME);
  11. properties.put("mail.smtp.auth", authenticate ? "true" : "false");
  12. final Authenticator authenticator = authenticate ? new SMTPAuthenticator() : null;
  13. final Session session = Session.getDefaultInstance(properties, authenticator);
  14. final Message message = new MimeMessage(session);
  15. final InternetAddress toAddress = new InternetAddress(toEmailAddress);
  16. message.setFrom(fromAddress);
  17. message.setRecipient(Message.RecipientType.TO, toAddress);
  18. message.setSubject("Expenses notification");
  19. message.setContent(text, "text/plain");
  20. Transport.send(message);
  21. } catch (AddressException e) {
  22. throw new ApplicationException("Invalid email address " + toEmailAddress, e);
  23. } catch (MessagingException e) {
  24. throw new ApplicationException("Problem sending email ", e);
  25. }
  26. }
  27. }

代码示例来源:origin: stackoverflow.com

  1. Message msg = new MimeMessage(mailSession);
  2. try {
  3. msg.setSubject(subject);
  4. msg.setText(body);
  5. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
  6. msg.setFrom();
  7. Transport t = session.getTransport();
  8. if (!t.isConnected()) {
  9. t.connect();
  10. }
  11. t.sendMessage(msg, null);
  12. } catch (MessagingException ex) {
  13. // Handle exception
  14. } catch (UnsupportedEncodingException ex) {
  15. // Handle exception
  16. }

代码示例来源:origin: org.mil-oss/fgsms-sla-processor

  1. boolean sendTest(String email) {
  2. log.log(Level.INFO, "fgsms SLA Pusher test message");
  3. try {
  4. Properties props = SLACommon.LoadSLAPropertiesPooled();
  5. Session mailSession = Session.getDefaultInstance(props);
  6. Message simpleMessage = new MimeMessage(mailSession);
  7. InternetAddress from;
  8. if (Utility.stringIsNullOrEmpty(email)) {
  9. from = new InternetAddress(props.getProperty("defaultReplyAddress"));
  10. } else {
  11. from = new InternetAddress(email);
  12. }
  13. InternetAddress to = new InternetAddress(email);
  14. simpleMessage.setFrom(from);
  15. simpleMessage.setRecipient(Message.RecipientType.TO, to);
  16. simpleMessage.setSubject("fgsms SLA Processor Test Message");
  17. simpleMessage.setContent("This is a test indicating that your fgsms SLA Processor is configured correctly." + "<br>"
  18. + "<a href=\"" + props.getProperty("fgsms.GUI.URL") + "\">fgsms</a>", "text/html; charset=ISO-8859-1");
  19. Transport.send(simpleMessage);
  20. return true;
  21. } catch (Exception ex) {
  22. log.log(Level.ERROR, "Error sending SLA alert email! " + ex.getLocalizedMessage());
  23. }
  24. return false;
  25. }
  26. private static String allitems = "All-Methods";

代码示例来源:origin: dee1024/housedb

  1. @Override
  2. public void send(String subject, String content){
  3. // 设置环境信息
  4. Session session = Session.getInstance(getPro());
  5. // 创建邮件对象
  6. Message msg = new MimeMessage(session);
  7. Transport transport = null;
  8. try {
  9. msg.setSubject(subject);
  10. // 设置邮件内容
  11. msg.setContent(content,"text/html;charset=utf8");
  12. // 设置发件人
  13. msg.setFrom(new InternetAddress(mailAccount)); //注意:此处设置SMTP发件人
  14. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(targetMail));
  15. transport = session.getTransport();
  16. // 连接邮件服务器
  17. transport.connect(mailAccount, mailPassword);
  18. // 发送邮件
  19. transport.sendMessage(msg, msg.getAllRecipients());
  20. transport.close();
  21. } catch (MessagingException e) {
  22. e.printStackTrace();
  23. }
  24. }

代码示例来源:origin: org.astrogrid/astrogrid-cea-server

  1. public void run() {
  2. final Map args = new HashMap();
  3. try {
  4. for (Iterator i = inputParameterAdapters(); i.hasNext();) {
  5. ParameterAdapter a = (ParameterAdapter)i.next();
  6. args.put(a.getWrappedParameter().getId(),a.process());
  7. }
  8. setStatus(Status.RUNNING);
  9. // send the message here.
  10. Session session = getSessionFromContext();
  11. Message message = new MimeMessage(session);
  12. message.setFrom(new InternetAddress("sendmail@builtin.astrogrid.org"));
  13. message.setSubject(args.get(SUBJECT).toString());
  14. message.setContent(args.get(MESSAGE).toString(),"text/plain");
  15. message.setRecipient(Message.RecipientType.TO,new InternetAddress(args.get(TO).toString()));
  16. Transport.send(message);
  17. setStatus(Status.COMPLETED);
  18. reportMessage("message sent");
  19. } catch (NamingException e) {
  20. reportError("Could not find mail session in JNDI",e);
  21. } catch (MessagingException e) {
  22. reportError("Failed to construct email message",e);
  23. } catch (Throwable t) {
  24. reportError("Something went wrong",t);
  25. }
  26. }

代码示例来源:origin: keeps/roda

  1. public void sendMail(String recipient, String message) throws MessagingException {
  2. if ("".equals(from)) {
  3. throw new MessagingException();
  4. }
  5. Session session = Session.getDefaultInstance(props, authenticator);
  6. session.setDebug(false);
  7. Message msg = new MimeMessage(session);
  8. InternetAddress addressFrom = new InternetAddress(from);
  9. msg.setFrom(addressFrom);
  10. msg.addHeader("name", fromActor);
  11. msg.setSubject(subject);
  12. InternetAddress recipientAddress = new InternetAddress(recipient);
  13. msg.setRecipient(Message.RecipientType.TO, recipientAddress);
  14. String htmlMessage = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%s", message);
  15. MimeMultipart mimeMultipart = new MimeMultipart();
  16. MimeBodyPart mimeBodyPart = new MimeBodyPart();
  17. mimeBodyPart.setContent(htmlMessage, "text/html;charset=UTF-8");
  18. mimeMultipart.addBodyPart(mimeBodyPart);
  19. msg.setContent(mimeMultipart);
  20. // sending the message
  21. SMTPTransport transport = (SMTPTransport) session.getTransport(this.protocol);
  22. transport.connect();
  23. transport.sendMessage(msg, msg.getAllRecipients());
  24. transport.close();
  25. }

相关文章