Guest User

Untitled

a guest
Dec 27th, 2017
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. public boolean sendMessage(String to, String msgSubject, String msgText)
  2. {
  3. String host = "mail.mydomain.com";
  4. String username = "myuser@mydomain.com"; // your authsmtp username
  5. String password = "mypassword" // your authsmtp password
  6. String from = "myuser@mydomain.com";
  7.  
  8. Properties props = System.getProperties();
  9. props.put("mail.smtp.host", host);
  10. props.put("mail.smtp.user", username);
  11. props.put("mail.smtp.password", password);
  12. props.put("mail.smtp.port", "25"); // thish is the port recommended by authsmtp
  13. props.put("mail.smtp.auth", "false");
  14.  
  15. Session session = Session.getDefaultInstance(props, null);
  16. MimeMessage message = new MimeMessage(session);
  17. message.setFrom(new InternetAddress(from));
  18.  
  19. InternetAddress to_address = new InternetAddress(to);
  20. message.addRecipient(Message.RecipientType.TO, to_address);
  21.  
  22. message.setSubject(msgSubject);
  23. message.setText(msgText);
  24. Transport transport = session.getTransport("smtp");
  25. transport.connect(host, username, password);
  26. transport.sendMessage(message, message.getAllRecipients());
  27. transport.close();
  28. return true;
  29. }
  30.  
  31. javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
  32.  
  33. at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:590)
  34.  
  35. at javax.mail.Service.connect(Service.java:291)
  36.  
  37. at javax.mail.Service.connect(Service.java:172)
  38.  
  39. at javax.mail.Service$connect.call(Unknown Source)
  40.  
  41. at org.helpdesk.MymailService.sendMessage(MymailService.groovy:37)
  42.  
  43. at org.helpdesk.MymailService$sendMessage.call(Unknown Source)
  44.  
  45. at org.helpdesk.RequestController$_closure13.doCall(RequestController.groovy:247)
  46.  
  47. at org.helpdesk.RequestController$_closure13.doCall(RequestController.groovy)
  48.  
  49. at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  50.  
  51. at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  52.  
  53. at java.lang.Thread.run(Unknown Source)
  54.  
  55. "mail.smtp.ehlo"
  56.  
  57. DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
  58. DEBUG SMTP: mechanism LOGIN not supported by server
  59. DEBUG SMTP: mechanism PLAIN not supported by server
  60. DEBUG SMTP: mechanism DIGEST-MD5 not supported by server
  61. DEBUG SMTP: mechanism NTLM not supported by server
  62.  
  63. class MymailService
  64. {
  65. boolean transactional = false
  66.  
  67. public sendMessage(String to, String cc, String msgSubject, String msgText)
  68. {
  69. String host = "mail.mailserver.com";
  70. String username = "myusername@mymailserver.com";
  71. String password = "xxx";
  72. String from = "myusername@mymailserver.com";
  73. String port = "25";
  74.  
  75. Properties props = System.getProperties();
  76. props.put("mail.smtp.host", host);
  77. props.put("mail.smtp.port", port);
  78. props.put("mail.smtp.auth", "false");
  79.  
  80. Transport transport = null;
  81.  
  82. try{
  83. Session session = Session.getDefaultInstance(props, null);
  84. MimeMessage message = new MimeMessage(session);
  85. message.setFrom(new InternetAddress(from));
  86.  
  87. InternetAddress to_address = new InternetAddress(to);
  88. message.addRecipient(Message.RecipientType.TO, to_address);
  89.  
  90. InternetAddress cc_address = new InternetAddress(cc);
  91. message.addRecipient(Message.RecipientType.CC, cc_address);
  92.  
  93. message.setSubject(msgSubject);
  94. message.setText(msgText);
  95.  
  96. transport = session.getTransport("smtp");
  97. transport.connect();
  98. transport.sendMessage(message, message.getAllRecipients());
  99. } finally {
  100. if (transport != null) try { transport.close(); } catch (MessagingException logOrIgnore){}
  101. }
  102. }
  103. }
  104.  
  105. package com.datereminder.service;
  106.  
  107. import java.util.Properties;
  108. import javax.mail.Message;
  109. import javax.mail.MessagingException;
  110. import javax.mail.PasswordAuthentication;
  111. import javax.mail.Session;
  112. import javax.mail.Transport;
  113. import javax.mail.internet.InternetAddress;
  114. import javax.mail.internet.MimeMessage;
  115.  
  116. public class ReminderDaemonService2 {
  117.  
  118. /**
  119. * @param args
  120. */
  121. public static void main(String[] args) {
  122. Properties props = new Properties();
  123. props.put("mail.smtp.host", "mail.mycompany123.com");
  124. // this mandates authentication at the mailserver
  125. props.put("mail.smtp.auth", "true");
  126. // this is for printing debugs
  127.  
  128. props.put("mail.debug", "true");
  129.  
  130.  
  131. Session session = Session.getDefaultInstance(props,
  132. new javax.mail.Authenticator() {
  133. protected PasswordAuthentication getPasswordAuthentication() {
  134. return new PasswordAuthentication("sadique.khan@mycompany123.com","xxxxxxxxxxx");
  135. }
  136. });
  137.  
  138. try {
  139.  
  140. Message message = new MimeMessage(session);
  141. message.setFrom(new InternetAddress("sadique.khan@mycompany123.com"));
  142. message.setRecipients(Message.RecipientType.TO,
  143. InternetAddress.parse("my.bestfriend@mycompany123.com"));
  144. message.setSubject("Testing Subject");
  145. message.setText("Dear Friend," +
  146. "nn This is a Test mail!");
  147.  
  148. Transport.send(message);
  149.  
  150.  
  151.  
  152. } catch (MessagingException e) {
  153. throw new RuntimeException(e);
  154. }
  155. }
  156. }
  157.  
  158. <bean id="mailSender" class="com.xxx.service.MailSender">
  159. <property name="host" value="${mail.host}" />
  160. <property name="port" value="${mail.port}" />
  161. <property name="protocol" value="${mail.protocol}" />
  162. <property name="defaultEncoding" value="UTF-8" />
  163. <property name="authRequired" value="${mail.auth}" />
  164. <property name="username" value="${mail.username}" />
  165. <property name="password" value="${mail.password}" />
  166. <property name="javaMailProperties">
  167. <props>
  168. <prop key="mail.smtps.auth">${mail.auth}</prop>
  169. </props>
  170. </property>
  171. </bean>
  172.  
  173. mail.from=XXX Team <xxx@tricascade.com>
  174. mail.host=exchange.xxx.com
  175. mail.port=25
  176. mail.protocol=smtp
  177. mail.auth=false
  178. mail.username=
  179. mail.password=
  180.  
  181. package com.xxx.service;
  182. import org.springframework.mail.javamail.JavaMailSenderImpl;
  183.  
  184. public class MailSender extends JavaMailSenderImpl {
  185.  
  186. private boolean authRequired;
  187.  
  188. @Override
  189. public String getUsername() {
  190. if (!authRequired) {
  191. return null;
  192. }
  193. return super.getUsername();
  194. }
  195.  
  196. @Override
  197. public String getPassword() {
  198. if (!authRequired) {
  199. return null;
  200. }
  201. return super.getPassword();
  202. }
  203.  
  204. public boolean isAuthRequired() {
  205. return authRequired;
  206. }
  207.  
  208. public void setAuthRequired(boolean authRequired) {
  209. this.authRequired = authRequired;
  210. }
  211.  
  212. }
  213.  
  214. props.put("mail.smtp.auth", false);
  215.  
  216. props.put("mail.smtp.auth", true);
Add Comment
Please, Sign In to add comment