Sending mail using SendGrid in Groovy - Grails - grails

I was using smtp server with Username password to send mail using the below function.
def sendEmail() {
AsynchronousMailMessage message = asyncMailService.sendMail {
// Mail parameters
to appointmentDetails.emailAddress
from AppConstant.EMAIL_SENDER
subject emailSubject
html emailMessage
}
}
SMTP details are stored in application.groovy.
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "XXXXXXXXXXXX#gmail.com"
password = "XXXXXXXXX"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
So this was working fine. Now our team wants to use SendGrid for achieve sending mails. I have got the API key, Key Name and other details from SendGrid Team and also have whitelisted my IP in sendGrid configuration. How can i implement that?
Have added below config in application.yml
sendgrid:
api: '${SENDGRID_APIKEY}'
from: '${SENDGRID_FROM_EMAIL}'

Related

Populating username & appname using parse-server-mandrill-adapter, not working

Im trying to use parse-server-mandrill-adapter for generating a new password for users who has forgot their passwords.
I am using a parse-server and I am using parse unbuilt function requestPasswordReset to generate a mail to the user. I manage to send the email successfully to the user using parse-server-mandrill-adapter, however I cant manage to get any data concerning the user included in this. E.g fields like username, appname do not populate.
Parse server index.js
emailAdapter: {
module: 'parse-server-mandrill-adapter',
options: {
// API key from Mandrill account
apiKey: process.env.MANDRILL_API_KEY || '',
// From email address
fromEmail: 'kontakt#bonsai.se',
// Display name
displayName: 'no-reply#bonsai.se',
// Reply-to email address
replyTo: 'no-reply#bonsai.se',
// Verification email subject
verificationSubject: 'Please verify your e-mail for *|appname|*',
// Verification email body
verificationBody: 'Hi *|username|*,\n\nYou are being asked to confirm the e-mail address *|email|* with *|appname|*\n\nClick here to confirm it:\n*|link|*',
// Password reset email subject
passwordResetSubject: 'Password Reset Request for *|appname|*',
// Password reset email body
passwordResetBody: 'Hi apa *|username|*,\n\nYou requested a password reset for *|appname|*.\n\nClick here to reset it:\n*|link|*'
}
The main.js file, where the requestPasswordReset function is called, with email, options and callback.
var query = new Parse.Query(Parse.User)
query.equalTo("email", request.params.email)
query.first({
useMasterKey: true
}).then(function (user) {
Parse.User.requestPasswordReset(request.params.email, {
options: {
"username": "test",
useMasterKey: true
}
}).then(function (success) {
console.log(success)
response.success("success")
}, function (error) {
console.log(error)
response.error("error")
});
})
The look of the email.
How the email looks
Take a look at : https://github.com/ParsePlatform/parse-server
Email verification and password reset
Also similar question & answer :
Parse open source server reset password error
I use the MailGun adapter which is included into the open-parse server and works like a charm!
I was able to use the parse-server-mandrill-adapter, and successfully reset user passwords with proper replacements to the email/appname/link:
1) Install the parse-server-mandrill-adapter, (add it to the package.json to be able to deploy it onto the remote server).
2) Update your index.js to support the emailAdapter, source link:
var server = ParseServer({
...
// App Name
appName: 'YourAppName',
// Environment where the user can confirm his e-mail address or reset his password (most likely the same as your 'serverURL')
publicServerURL: 'YourPublicServerURL',
emailAdapter: {
module: 'parse-server-mandrill-adapter',
options: {
// API key from Mandrill account
apiKey: 'API-KEY',
// From email address
fromEmail: 'no-reply#yourdomain.com',
// Display name
displayName: 'no-reply#yourdomain.com',
// Reply-to email address
replyTo: 'no-reply#yourdomain.com',
// Verification email subject
verificationSubject: 'Please verify your e-mail for *|appname|*',
// Verification email body
verificationBody: 'Hi *|username|*,\n\nYou are being asked to confirm the e-mail address *|email|* with *|appname|*\n\nClick here to confirm it:\n*|link|*',
// Password reset email subject
passwordResetSubject: 'Password Reset Request for *|appname|*',
// Password reset email body
passwordResetBody: 'Hi *|username|*,\n\nYou requested a password reset for *|appname|*.\n\nClick here to reset it:\n*|link|*'
}
}
...
});
3) The password reset code:
Parse.User.requestPasswordReset(userEmail, {
success: function() {
// success
},
error: function(error) {
// error
}
});
4) Make sure when running that code, that your app is running from the remote server, it failed for me when running on localhost:1337!

Authentication failure when using mail Grails plugin

i'm using Grails 2.5.1 and mail:1.0.7 plugin to send emails , but when i'm using it i always getting the bellow error :
Class:javax.mail.AuthenticationFailedExceptionMessage:535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/answer/14257 qq4sm4579366wjc.14 - gsmtp
although i can login with the provided credentials successfully from the browser !!
here are my configurations in the Config file :
grails {
mail {
host = "smtp.gmail.com"
port =465
username = "****"
password = "***"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
and here is the action :
mailService.sendMail{
to userInstance.toEmail
subject userInstance.subject
html "<strong>Test</strong> "
}
anything i'm missing ?
It may be because of gmail's security feature for less secure apps to get authenticated. Just turn on the access for less secure apps.
Follow below steps:
1 Login to your gmail account
2 Access the url:
https://www.google.com/settings/security/lesssecureapps
3 Select "Turn on"
If you have gmail - settings for plugin are correct (i have the same and they are working) , but if your email is not gmail you must have other config.
By the way if you gmail :
Username and Password not accepted.
your username or password is wrong, try to login with this Username and password, watch out for register of letters.
https://grails.org/plugin/mail

ERROR Connection refused: Grails email verification using mail plugin

i am new to Grails.
i am using mail 1.0 pluguin..
i configured config file as below
grails {
mail {
host = "smtp.gmail.org"
port = 465
username = "myaccount#gmail.com"
password = "mypassword"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
and used sendMail() in controller as below
sendMail {
to "myanotheraccount#gmail.com"
subject "New User Confirmation"
body "how are u"
}
I have no firewall issues.
but it show s error as Connection refused.......???
Probably a configuration issue, have you looked at this example which shows how to use Mailgun via SMTP: https://github.com/jbayer/mail-java

Grails Send Mail is not working

I am using Mail Plugin to send email in my grails application. I am doing like this ...
Config.groovy ----
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "example#gmail.com"
password = "*********"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
} }
and Controller ---
class MailController {
def mailService
def sendmail = {
mailService.sendMail {
to "example#gmail.com"
subject "Hello Fred"
body 'How are you?'
}
}
When I am trying to send mail. It throwing ERROR
URI
/groovypublish/mail/sendmail
Class
sun.security.provider.certpath.SunCertPathBuilderException
Message
unable to find valid certification path to requested target
If I remove mail props part from my config.groovy. Then After send mail, my page loaded infinite times.
I am using localhost:8080 to send mail. I know problem is in SSL. But How can I avoid SSL part.
Please help ...
I had the same issue. Going into my virus scanner's settings and turning off outbound mail scanning solved the issue.

Sending email using smtp works only after sending it by outlook

I'm trying to send email using asp.net mvc application. Smtp client is configured in web.config e.g.:
<mailSettings>
<smtp from="noreply#test.sk">
<network host="mail.test.sk" defaultCredentials="false" userName="noreply#test.sk" password="pass" port="25"/>
</smtp>
</mailSettings>
C#:
using (SmtpClient client = new SmtpClient())
{
MailMessage message = new MailMessage(from, to, subject, body);
client.Timeout = 10000;
client.Send(message);
return false;
}
The thing is, it doesn't work (I get timeout exception) until I try to send email using outlook. Right after sending email by outlook it successfully sends it also by my web application. Is there some special kind of authentication which outlook is doing which then allows all emails from my IP to be authenticated?? What is the reason it works only after sending it by outlook?
BTW:
I'm running the application in VS asp.net development server. When I deploy it to webhosting server, it doesn't work (timeout).
My webhosting provider told me there is a classic smtp authentication on that server (not pop3 before smtp).
EDITED:
I figured out, when it worked and I tried it by telnet or traced it by wireshark, the communication started by:
220 mail2.hostmaster.sk ESMTP Postfix
EHLO fernet-PC
250-mail2.hostmaster.sk
250-PIPELINING
250-SIZE 104857600
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
...
Compared to when it didn't work (just tried a day before), the whole communication looked like:
220-mail2.hostmaster.sk ESMTP Postfix
EHLO fernet-PC
250-mail2.hostmaster.sk
250-SIZE 104857600
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
So there is a difference in supported auth methods and support for pipelining. I guess that System.Net.Mail.SmtpClient then doesn't know which authentication method should it use and so it stucks and therefore I'm getting the timeout exception.
That's interesting that outlook has no problems to connect and send email.
I solved my problem by not using their smtp server, as I'm running out of time. I'm using gmail's smtp server instead. I created new gmail account where I set alias to what I needed, so the email looks like it didn't come from gmail on the first look.
I have fixed a similar problem on a site and came across this Question while researching the issue.
The code below works for Google Apps for Business;
public class SendSMTPEmail
{
public static void SendText(string ToName, string ToEmail, string FromName, string FromEmail, string Subject, string Content)
{
MailAddress from = new MailAddress(ToEmail, ToName);
MailAddress to = new MailAddress(ToEmail, ToName);
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("username#googlemail.com", "yourpassword"),
Timeout = 20000
};
MailMessage message = new MailMessage()
{
From = from,
Body = Content,
Subject = Subject
};
message.To.Add(to);
smtp.Send(message);
}
}
Does this help?

Resources