Hybris EmailService where configure credentials - email

I'm using EmailService in order to send a basic email:
String subject = request.getParameter("subject");
String body = request.getParameter("body");
EmailAddressModel fromAddress = modelService.create(EmailAddressModel.class);
fromAddress.setDisplayName("test#test.es");
fromAddress.setEmailAddress("test#test.es");
List<EmailAddressModel> addresses = new ArrayList<>();
addresses.add(fromAddress);
EmailMessageModel email = modelService.create(EmailMessageModel.class);
email.setSubject(subject);
email.setBody(body);
email.setToAddresses(addresses);
email.setFromAddress(fromAddress);
email.setReplyToAddress("myaccount#gmail.com");
modelService.save(email);
emailService.send(email);
Where do I configure the password of "myaccount#gmail.com" in order to make the Hybris SMTP server authentificate with my personal mail and send the email?

You can configure below parameters in local.properties file or you can change them in hac for runtime only.
mail.smtp.server
mail.smtp.port
mail.smtp.user
mail.smtp.password
mail.pop3.beforesmtp
mail.pop3.password
mail.pop3.server
mail.pop3.user
mail.from
mail.replyto
mail.use.tls
You can get more detail from blog post.

Related

How to run a cgi program as a google authorised user - as 3rd party apps are now forbidden [duplicate]

So since the 31 of may google has disabled the option for "Less secure apps", so I have been using the Java mail API, and since the update i can no longer send emails using the Gmail smtp.
This is the error I'm getting:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp
I switched to an outlook mail and it seems to work fine, but i was wondering if there is a way to use a Gmail account
Less secure apps & your Google Account
You could try authentification via "App password".
On your Google account:
set 2-Step Verification ON
create 16-character "App password"(
How to create app password) -> result should be similar to:
16-character "App password"
Instead of Google account password use 16-character password
MailMessage mail = new MailMessage();
foreach (string receiver in DolociPrejemnike())
mail.To.Add(receiver);
mail.From = new MailAddress("app_gmail#gmail.com", "No replay"); //poĊĦiljatelj (vedno enak)
mail.Subject = SetSubject();
mail.Body = SetBody();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("app_gmail#gmail.com", "xtqapucsmyvqmvxp"); // Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
So thanks for all the replays! i have fixed this issue by doing this:
I have enabled the "App password for windows machines"
Then i simply changed the password from the email password to the google generated one
and changed the code to the following:
public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {
Properties properties=new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
String myAccountEmail="yourEmailAddress#gmail.com";
String password="Generated Windows machine password from google";
Session session=Session.getInstance(properties, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, password);
}
});
Message message=prepareMessage(session,myAccountEmail,recepient,order);
Transport.send(message);
System.out.println("Message Sent successfully");
}
private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
message.setSubject("Your subject here");
message.setText(");
return message;
} catch (AddressException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Now that you can no longer use login and password with Googles smtp server the only option really is to use XOauth2
I havent used Jakarta before but it appears to support it. You should look into OAuth2 Support
Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);
Apps password
option two is to go to your google account and generate an apps password
When running your code use the password generated instead of the actual users password. The main issue with this being there is no telling how long google will continue to support apps password.
To those who followed the other answers but are still getting the "Authentication Failed" error when using an app password, a key point is that this solution is NOT working for XOAUTH2 if you are using that or are following a guide saying to use oauth2.
So in the following code:
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Simply change it to the following:
props.put("mail.imap.auth.mechanisms", "XOAUTH");
and it should work, keeping all else the same.
Note: please enable 2-factor authentication in google account before proceeding.
Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) options is no longer available.
Instead use another way of using apppasswords provided by google.
https://myaccount.google.com/u/0/apppasswords
Use 16 digit code provided by google instead of password and that will serve as authentication token.

Is there a work around google disabling "Less secure apps"?

So since the 31 of may google has disabled the option for "Less secure apps", so I have been using the Java mail API, and since the update i can no longer send emails using the Gmail smtp.
This is the error I'm getting:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp
I switched to an outlook mail and it seems to work fine, but i was wondering if there is a way to use a Gmail account
Less secure apps & your Google Account
You could try authentification via "App password".
On your Google account:
set 2-Step Verification ON
create 16-character "App password"(
How to create app password) -> result should be similar to:
16-character "App password"
Instead of Google account password use 16-character password
MailMessage mail = new MailMessage();
foreach (string receiver in DolociPrejemnike())
mail.To.Add(receiver);
mail.From = new MailAddress("app_gmail#gmail.com", "No replay"); //poĊĦiljatelj (vedno enak)
mail.Subject = SetSubject();
mail.Body = SetBody();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("app_gmail#gmail.com", "xtqapucsmyvqmvxp"); // Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
So thanks for all the replays! i have fixed this issue by doing this:
I have enabled the "App password for windows machines"
Then i simply changed the password from the email password to the google generated one
and changed the code to the following:
public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {
Properties properties=new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
String myAccountEmail="yourEmailAddress#gmail.com";
String password="Generated Windows machine password from google";
Session session=Session.getInstance(properties, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, password);
}
});
Message message=prepareMessage(session,myAccountEmail,recepient,order);
Transport.send(message);
System.out.println("Message Sent successfully");
}
private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
message.setSubject("Your subject here");
message.setText(");
return message;
} catch (AddressException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Now that you can no longer use login and password with Googles smtp server the only option really is to use XOauth2
I havent used Jakarta before but it appears to support it. You should look into OAuth2 Support
Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);
Apps password
option two is to go to your google account and generate an apps password
When running your code use the password generated instead of the actual users password. The main issue with this being there is no telling how long google will continue to support apps password.
To those who followed the other answers but are still getting the "Authentication Failed" error when using an app password, a key point is that this solution is NOT working for XOAUTH2 if you are using that or are following a guide saying to use oauth2.
So in the following code:
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Simply change it to the following:
props.put("mail.imap.auth.mechanisms", "XOAUTH");
and it should work, keeping all else the same.
Note: please enable 2-factor authentication in google account before proceeding.
Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) options is no longer available.
Instead use another way of using apppasswords provided by google.
https://myaccount.google.com/u/0/apppasswords
Use 16 digit code provided by google instead of password and that will serve as authentication token.

Serilog Email sink

I'm trying to send emails using Serilog.Sinks.Email NuGet package (v1.5.0.0) with the Mandrill SMTP service. The following code executes but does not send any emails. When I try and use the same credentials using the System.Net.Mail.SmtpClient, it works and send an email.
EmailConnectionInfo info = new EmailConnectionInfo()
{
EmailSubject = "Email subject",
FromEmail = "from#gmail.com",
MailServer = "smtp.mandrillapp.com",
NetworkCredentials = new NetworkCredential("mandrill_username", "mandrill_apikey"),
Port = 587,
ToEmail = "to#gmail.com"
};
Log.Logger = new LoggerConfiguration()
.WriteTo.Email(info)
.CreateLogger();
Log.Error("Houston we have a problem");
As you spotted, this was a bug in the latest build of the Email sink, which your graciously-provided pull request has fixed. Version 1.5.13 of the sink, now on NuGet, includes the fix.

How to send mail based on from email address using smtp c#

We want to send email using from email address instead of smtp email address...
I tried to send mail where from email address and smtp authenticated email address are different.
It gives me error.
You can based on the following code below. Hope it helps
MailMessage mailMessage = new MailMessage();
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress("sender#domain.com", "Subject"); // You can try changing this to the email address you want
mailMessage.ReplyToList.Add("sender#domain.com"); // Here you can add reply to
mailMessage.Subject = "ENQUIRY - " + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
mailMessage.Body = ""; // The body of email
SmtpClient smtpClient = new SmtpClient("mail.company-domain.com");
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Port = 587; // We used this port instead of port 25
smtpClient.Send(mailMessage);

Writing an encrypted mail via Exchange Web Services

I would like to send an encrypted EMail Message with Exchange WEb Services using C#.
Is there any possibillity?
Thanks
Edit:
My Mail body encrypter:
public static byte[] encry(string body, ContentTyp typ, string to )
{
X509Certificate2 cert = GetMailCertificate(to);
StringBuilder msg = new StringBuilder();
msg.AppendLine(string.Format("Content-Type: text/{0}; charset=\"iso-8859-1\"", typ.ToString()));
msg.AppendLine("Content-Transfer-Encoding: 7bit");
msg.AppendLine();
msg.AppendLine(body);
EnvelopedCms envelope = new EnvelopedCms(new ContentInfo(Encoding.UTF8.GetBytes(msg.ToString())));
CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, cert);
envelope.Encrypt(recipient);
//System.IO.MemoryStream ms = new System.IO.MemoryStream(envelope.Encode());
return envelope.Encode();
}
Main
byte [] con = encrypted.encry("test", encrypted.ContentTyp.plain, "test#server.com");
EmailMessage msg1 = new EmailMessage(_server);
msg1.MimeContent = new MimeContent("UTF-8", con);
msg1.ToRecipients.Add("user#server.com");
msg1.InternetMessageHeaders = ??
msg1.Send();
If you are referring to S/Mime encryption, then you'll have to create the encrypted message according to RFC 3852 and RFC 4134. After you've done that, you can send the message.
Using the EWS Managed API, this can be done as follows:
var item = new EmailMessage(service);
item.MimeContent = new MimeContent(Encoding.ASCII.HeaderName, content);
// Set recipient infos, etc.
item.Send();
EDIT:
You should add the standard headers like From, To, Date, Subject, etc. And the content-type.
Subject: Test
From: "sender" <sender#yourcompany.com>
To: "recipient" <recipient#othercompany.com>
Content-Type: application/pkcs7-mime; smime-type=signed-data; name=smime.p7m
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=smime.p7m
Your encrypted body goes here
Just use a StringWriter put all that together. The result is your MIME body.