Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean sendMessage(String to, String msgSubject, String msgText)
- {
- String host = "mail.mydomain.com";
- String username = "myuser@mydomain.com"; // your authsmtp username
- String password = "mypassword" // your authsmtp password
- String from = "myuser@mydomain.com";
- Properties props = System.getProperties();
- props.put("mail.smtp.host", host);
- props.put("mail.smtp.user", username);
- props.put("mail.smtp.password", password);
- props.put("mail.smtp.port", "25"); // thish is the port recommended by authsmtp
- props.put("mail.smtp.auth", "false");
- Session session = Session.getDefaultInstance(props, null);
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(from));
- InternetAddress to_address = new InternetAddress(to);
- message.addRecipient(Message.RecipientType.TO, to_address);
- message.setSubject(msgSubject);
- message.setText(msgText);
- Transport transport = session.getTransport("smtp");
- transport.connect(host, username, password);
- transport.sendMessage(message, message.getAllRecipients());
- transport.close();
- return true;
- }
- javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
- at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:590)
- at javax.mail.Service.connect(Service.java:291)
- at javax.mail.Service.connect(Service.java:172)
- at javax.mail.Service$connect.call(Unknown Source)
- at org.helpdesk.MymailService.sendMessage(MymailService.groovy:37)
- at org.helpdesk.MymailService$sendMessage.call(Unknown Source)
- at org.helpdesk.RequestController$_closure13.doCall(RequestController.groovy:247)
- at org.helpdesk.RequestController$_closure13.doCall(RequestController.groovy)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
- at java.lang.Thread.run(Unknown Source)
- "mail.smtp.ehlo"
- DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
- DEBUG SMTP: mechanism LOGIN not supported by server
- DEBUG SMTP: mechanism PLAIN not supported by server
- DEBUG SMTP: mechanism DIGEST-MD5 not supported by server
- DEBUG SMTP: mechanism NTLM not supported by server
- class MymailService
- {
- boolean transactional = false
- public sendMessage(String to, String cc, String msgSubject, String msgText)
- {
- String host = "mail.mailserver.com";
- String username = "myusername@mymailserver.com";
- String password = "xxx";
- String from = "myusername@mymailserver.com";
- String port = "25";
- Properties props = System.getProperties();
- props.put("mail.smtp.host", host);
- props.put("mail.smtp.port", port);
- props.put("mail.smtp.auth", "false");
- Transport transport = null;
- try{
- Session session = Session.getDefaultInstance(props, null);
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(from));
- InternetAddress to_address = new InternetAddress(to);
- message.addRecipient(Message.RecipientType.TO, to_address);
- InternetAddress cc_address = new InternetAddress(cc);
- message.addRecipient(Message.RecipientType.CC, cc_address);
- message.setSubject(msgSubject);
- message.setText(msgText);
- transport = session.getTransport("smtp");
- transport.connect();
- transport.sendMessage(message, message.getAllRecipients());
- } finally {
- if (transport != null) try { transport.close(); } catch (MessagingException logOrIgnore){}
- }
- }
- }
- package com.datereminder.service;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class ReminderDaemonService2 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Properties props = new Properties();
- props.put("mail.smtp.host", "mail.mycompany123.com");
- // this mandates authentication at the mailserver
- props.put("mail.smtp.auth", "true");
- // this is for printing debugs
- props.put("mail.debug", "true");
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("sadique.khan@mycompany123.com","xxxxxxxxxxx");
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("sadique.khan@mycompany123.com"));
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse("my.bestfriend@mycompany123.com"));
- message.setSubject("Testing Subject");
- message.setText("Dear Friend," +
- "nn This is a Test mail!");
- Transport.send(message);
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- }
- <bean id="mailSender" class="com.xxx.service.MailSender">
- <property name="host" value="${mail.host}" />
- <property name="port" value="${mail.port}" />
- <property name="protocol" value="${mail.protocol}" />
- <property name="defaultEncoding" value="UTF-8" />
- <property name="authRequired" value="${mail.auth}" />
- <property name="username" value="${mail.username}" />
- <property name="password" value="${mail.password}" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtps.auth">${mail.auth}</prop>
- </props>
- </property>
- </bean>
- mail.from=XXX Team <xxx@tricascade.com>
- mail.host=exchange.xxx.com
- mail.port=25
- mail.protocol=smtp
- mail.auth=false
- mail.username=
- mail.password=
- package com.xxx.service;
- import org.springframework.mail.javamail.JavaMailSenderImpl;
- public class MailSender extends JavaMailSenderImpl {
- private boolean authRequired;
- @Override
- public String getUsername() {
- if (!authRequired) {
- return null;
- }
- return super.getUsername();
- }
- @Override
- public String getPassword() {
- if (!authRequired) {
- return null;
- }
- return super.getPassword();
- }
- public boolean isAuthRequired() {
- return authRequired;
- }
- public void setAuthRequired(boolean authRequired) {
- this.authRequired = authRequired;
- }
- }
- props.put("mail.smtp.auth", false);
- props.put("mail.smtp.auth", true);
Add Comment
Please, Sign In to add comment