javaMail with Lotus server - java

im facing some problems with Lotus server.
The guy that is in charge of the server is telling me that the configuration is ok, but i cant send mail with html body with his lotus server.
The error i get is : “554 Relay rejected for policy reasons.”
When i tried on my pc, i used smpt.gmail.com and worked like a champ. So i believe is not a code problem and the issue is with the server configuration.
Is there a problem with javaMail and Lotus? is it a common issue? (in one blog some guy was saying that it can not be possible to send html but i cant believe that)
My code just in case,
public void sendEmail(String toEmailAddr, String subject, String issue) {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress toAddress = null;
InternetAddress toAddress2[] = null;
Transport t = null ;
try {
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(issue, "text/html");
mp.addBodyPart(htmlPart);
simpleMessage.setContent(mp);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
toAddress = new InternetAddress(toEmailAddr);
toAddress2 = new InternetAddress [1];
toAddress2[0] = toAddress;
} catch (AddressException e) {
// TODO LOG
e.printStackTrace();
}
try {
simpleMessage.setRecipients(RecipientType.TO, toAddress2);
simpleMessage.setSubject(subject);
t = mailSession.getTransport("smtp");
if(userPwd==null)
userPwd = "";
t.connect(host, userName, userPwd);
t.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
} catch (MessagingException e) {
e.printStackTrace();
// TODO LOG
}finally{
try {
t.close();
} catch (MessagingException e) {
// TODO LOG
}
}
}
Regards.

SMTP on the Domino server has most likely been set up to only allow relay by certain hosts - therefore the error message 554 Relay rejected for policy reasons.
You should talk to the admin and have him change the configuration to allow relay by other hosts. This is configured in a configuration document in the Router/SMTP -> Restrictions and Controls -> SMTP Inbound Controls section. More information on SMTP inbound relay controls is available here:
http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/index.jsp?topic=%2Fcom.ibm.help.domino.admin.doc%2FDOC%2FH_SETTING_INBOUND_RELAY_CONTROLS_STEPS.html

i had the same problem and solved it. The FROM part was "me#example.com" and changed it to "myname#mydomain.com" and it began to send

May require Secure Connection(SSL), Use the following properties to connect mail server supporting smtp protocol:
properties.put("mail.smtp.socketFactory.port", "SMTP_PORT");
properties.put("mail.smtp.host", "SMTP_SERVER_HOST_NAME_OR_IP");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");

Related

Save Sent email to sent items folder using javax mail

public static void sendEmail(String msgHeader, String msg, String emailId, String emailFrom) {
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", mailServer);
props.put("mail.smtp.port", port#);
props.put("mail.smtp.auth.mechanisms", "NTLM");
props.put("mail.smtp.auth.ntlm.domain", domainName);
Session session = Session.getDefaultInstance(props, null);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailFrom));
to = emailId;
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(msgHeader);
message.setText(msg, "utf-8", "html");
message.saveChanges();
session.getDebug();
Transport.send(message);
// Copy message to "Sent Items" folder as read
Store store = session.getStore("ntlm");
store.connect(mailServer, emailFrom, pwd);
Folder folder = store.getFolder("Sent Items");
folder.open(Folder.READ_WRITE);
message.setFlag(Flag.SEEN, true);
folder.appendMessages(new Message[] {message});
store.close();
} catch (Exception ex) {
logger.error("Error occured while sending Email !", ex);
}
}
When I try to execute the code above, i am able to send out the emails. the issue is with saving the email. I get an error (NoSuchProviderException) at the line
Store store = session.getStore("ntlm");
I have a few questions on this:-
The email sending part works without password verification with ntlm. Is it possible to save the sent email into the sent items folder without password verification. If yes then how?
session.getStore doesnt work when i use
a. smtp - exception (Invalid provider)
b. ntlm - exception (NoSuchProviderException)
what should i use here.
Thanks in advance for your help.
"ntlm" is not a type of Store, it's an authentication mechanism. The store types supported by JavaMail are "imap" and "pop3". You almost certainly want "imap". Just like sending, you're going to need to supply your username and password when connecting to your imap server.
Also, upgrade to the current version of JavaMail if possible.

Javax mail sendpulse smtp server

I'm trying to setup Javax.mail to work with sendpulse smtp server (actually had different prob with other also).
currently it's always just hangs here System.out.println("\n\n 3rd ===> Get Session and Send mail");. No exceptions (even i setup a timeouts), nothing.
Also, debug shown me that transport contains login value as mypcname#smtp-pulse.com, which is not right. and the defaultPort = 465 (instead of 2525 set by me in properties)
smtp serve configured by input string smtp-pulse.com,2525,maemail#yahoo.com,mypass ->
the current method to send (tried a lot of different options):
public SMTPresponce SendHtmlEmail(String receipient){
Integer timeout = 20000;
logger.info("Sending to: "+receipient);
// Step1
System.out.println("\n 1st ===> setup Mail Server Properties..");
mailServerProperties = System.getProperties();
mailServerProperties.setProperty("mail.transport.protocol", "smtp");
mailServerProperties.setProperty("mail.smtp.host", accountSMTP.getHost());
mailServerProperties.setProperty("mail.smtp.port", accountSMTP.getPort());
mailServerProperties.setProperty("mail.smtp.starttls.enable", "true");
mailServerProperties.setProperty("mail.smtp.ssl.enable", "true");
mailServerProperties.setProperty("mail.smtp.ssl.trust", "*");
mailServerProperties.setProperty("mail.smtp.auth", "true");
mailServerProperties.setProperty("mail.smtp.timeout", timeout.toString());
mailServerProperties.setProperty("mail.smtp.connectiontimeout", timeout.toString());
try {
System.out.println("\n\n 2nd ===> get Mail Session..");
getMailSession = Session.getDefaultInstance(mailServerProperties, new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(accountSMTP.getLogin(), accountSMTP.getPassword());
}
});
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receipient));
generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("test#mail.com"));
try {
generateMailMessage.setFrom(new InternetAddress(messageMy.getFinalFrom(), messageMy.getReply_to()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receipient));
generateMailMessage.setSubject(messageMy.getFinalTopic());
generateMailMessage.setContent(messageMy.getFinalBody(), "text/html; charset=utf-8");
// Send message
// Step3
System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");
// Enter your correct gmail UserID and Password
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
return SMTPresponce.SEND_SUCCESS;
}
catch (AuthenticationFailedException ex){
return SMTPresponce.AUTH_FAIL;
}
catch (MessagingException mex) {
logger.error(mex.getCause());
mex.printStackTrace();
if(mex.getCause().toString().contains("Connection timed out"))
return SMTPresponce.TIMEOUT;
return SMTPresponce.SEND_FAIL;
}
}

Sending email using godaddy account using java

I am trying to send emails using my godaddy account in java. Below is my code.
Properties props = System.getProperties();
props.put("mail.transport.protocol","smtp" );
props.put("mail.smtp.starttls.enable","true" );
props.put("mail.smtp.ssl.enable", "false");
props.put("mail.smtp.host","smtpout.secureserver.net");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
props.put("mail.debug","true");
props.put("mail.smtp.socketFactory.port","465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
Authenticator auth = new SMTPAuthenticator();
Session session=Session.getInstance(props,auth);
session.setDebug(true);
// -- Create a new message --
Transport transport=session.getTransport();
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(""email#domain.com));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("email#domain.com", false));
msg.setSubject("subject");
msg.setText("Message");
transport.connect();
Transport.send(msg);
transport.close();
While executing I'm getting the below Exception.
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtpout.secureserver.net , 465; timeout -1;
nested exception is:
java.net.UnknownHostException: smtpout.secureserver.net
PS:When i use gmail account for authentication its working fine and email sent successfully. When i use godaddy account the exception throws.
Please guide me how to solve this issue...
Thanks in advance...
My configuration in SpringBoot (replace domainname.com to your domainname)
spring:
mail:
host: smtpout.secureserver.net
port: 465
username: info#domainname.com
password: password
protocol: smtp
properties.mail.smtp:
auth: true
ssl.enable: true
ssl.trust: "*"
Also I had to add mimeMessageHelper.setFrom("info#domainname.com"); before sending the mail (else it was taking my system name and gave an error) and this setup worked.
Here is complete method that is working absolutely fine for me (I have also used an attachment in the message) :
private void sendUsingSmtp(MailDetail mailDetail) {
Properties props = new Properties();
props.put("mail.host", "smtpout.secureserver.net");
props.put("mail.port", "465");
props.put("mail.username", "info#domainName.com");
props.put("mail.password", “password”);
props.put("mail.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.trust", "*");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("info#domainName”, “password");
}
});
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("info#domainName.com", false);
msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(“targetEmail#gmail.com"));
msg.setSubject("Test Subject.");
msg.setContent("Test Content.", "text/html");
msg.setSentDate(new Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("Test Content.", "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile("/var/tmp/abc.txt");
multipart.addBodyPart(attachPart);
msg.setContent(multipart);
Transport.send(msg);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JavaMail SSL with no Authentication trust certificate regardless
MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
socketFactory.setTrustAllHosts(true);
requires javax.mail 1.5.2 and greater

Javamail rate for the client exceeded

I need to send at least 200 messages from a stretch. When the program starts, send mail successfully to 15 or 17, then I get this error:
MESSAGE ERROR:
com.sun.mail.smtp.SMTPSendFailedException: 421 4.4.2 Message submission rate for this client has exceeded the configured limit
What I can do?
CODE JAVA
public void mandarEmail(String correos, String mensaje, String asunto) {
Message message;
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.port", "587");
props.put("mail.smtp.host", "pod51004.outlook.com");
props.put("mail.smtp.debug", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("docemail#usmp.pe", "docpass");
}
});
try {
message = new MimeMessage(session);
message.setFrom(new InternetAddress("USMP - FN <documentos-fn#usmp.pe>"));
message.setSubject(asunto);
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(correos));
message.addRecipients(Message.RecipientType.BCC, new InternetAddress[]{new InternetAddress("ivan_pro_nice#hotmail.com")});
message.setContent(mensaje, "text/html; charset=utf-8");
Transport transport = session.getTransport("smtp");
transport.connect("docemail#usmp.pe", "docpass");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
throw new RuntimeException(e);
} finally {
props = null;
message = null;
}
}
That's the server you're connecting to, and not a client issue. Here's a doc on how to parse SMTP codes from the server.
A mail server will reply to every request a client (such as your email
program) makes with a return code. This code consists of three
numbers.
In your case, you're getting 421.
You probably need to pay for a "business" account from your mail server vendor so they'll let you send more email.
if you want to send single email to 200 clients. Than u can add an array of reciever's email addresses of size upto 50.
but i you want to send different msg for each email. Then you can create a new connection to email server with a counter that counts send emails as well as it counts 15 it should create a new connection.
to test your code use mailtrap.io

send email with SMTP command, using socket programming in Java

I am trying to send email via socket programming using SMTP commands using socket in java. But i am unable to do so. The problem is in authentication may be.
I need SMTP commands to send email and to authenticate user over server.
Any help appreciated.
Thanks in Advance
Imran
Create Transport Object
Properties props = new Properties();
props.setProperty("mail.transport.protocol", pProtocol);
props.setProperty("mail.host", pHost);
props.setProperty("mail.user", pUser);
props.setProperty("mail.password", pPassword);
mMailSession = Session.getDefaultInstance(props, null);
mMailSession.setDebug(true);
try {
mTransport = mMailSession.getTransport();
mTransport.connect();
} catch (MessagingException e) {
mLog.error(e.getMessage(), e);
throw new MailException(e);
}
Send mail
try {
MimeMessage message = new MimeMessage(mMailSession);
message.setSubject(pSubject);
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(pContent, "text/html");
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
message.setContent(mp);
message.addFrom(new Address[] { new InternetAddress(pFrom) });
for (int i = 0; i < pTo.length; i++) {
String tTo = pTo[i];
message.addRecipient(Message.RecipientType.TO, new InternetAddress(tTo));
}
mTransport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
} catch (MessagingException me) {
throw new MailException(me);
}
EDIT:
After the comment from Thilo, i would like to append, that this solution depend on com.sun.mail and imports javax.mail.* classes.

Categories