Changing mail configuration in runtime - spring

I've just started to investigate grails framework and first task I'm trying to resolve is sending email. Basic tutorial and community answers provided lot of information for quick start, and yes I've created simple app with possibility to send email. But, next point of my investigation was changing mail configuration in runtime. So, first my configuration in Config.grovy was
grails {
mail {
host = ""
port = 0
username = ""
password = ""
props = [""]
}
}
with values, and all worked corectlly, after that I've tried to re-config it
like that
grailsApplication.config.grails.mail.host = "smtp.gmail.com"
grailsApplication.config.grails.mail.port = 465
grailsApplication.config.grails.mail.username = ""
from controller, and found that mail is sending from old adress, after debugs I've found that there are auto-wired instances in mail plugins like mailSender and one obvious solution is recreate mailSender and re-set it, but judging to Spring singleton policy it will be hard solution, so, My question Are there possibilities to re-configure mail in runtime without class-reloading ?
Thanks.

Fixed using re-init mailsender instance but waiting for other solutions, Thanks
mailSender.setHost("smtp.gmail.com")
mailSender.setPort(465)
mailSender.setJavaMailProperties(new Properties() {
{
put("mail.smtp.auth", "true");
put("mail.smtp.socketFactory.port", "465");
put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
put("mail.smtp.socketFactory.fallback", "false");
}
})

try following way
sendMail {
mailSender.username = 'abc#gmail.com'
mailSender.password = "pwd"
to "nagaraj.s#hotmail.com"
subject "Hi"
message "Hi"
}

Related

Cannot make XBAP cookies work

I am trying to make a XBAP application communicating with a webservice with login.
But I want the user to skip the login step if they already logged in the last seven days.
I got it to work using html/aspx.
But it fails continuously with XBAP.
While debugging, the application is given full trust.
This is the code I have so far to write the cookie:
protected static void WriteToCookie(
string pName,
Dictionary<string, string> pData,
int pExiresInDays)
{
// Set the cookie value.
string data = "";
foreach (string key in pData.Keys)
{
data += String.Format("{0}={1};", key, pData[key]);
}
string expires = "expires=" + DateTime.Now.AddDays(pExiresInDays).ToUniversalTime().ToString("r");
data += expires;
try
{
Application.SetCookie(new Uri(pName), data);
}
catch (Exception ex)
{
}
}
And this is what I have to read the cookie:
protected static Dictionary<string, string> ReadFromCookie(
string pName)
{
Dictionary<string, string> data = new Dictionary<string, string>();
try
{
string myCookie = Application.GetCookie(new Uri(pName));
// Returns the cookie information.
if (String.IsNullOrEmpty(myCookie) == false)
{
string[] splitted = myCookie.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
string[] sub;
foreach(string split in splitted)
{
sub = split.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (sub[0] == "expires")
{
continue;
}
data.Add(sub[0], sub[1]);
}
}
}
catch(Exception ex)
{
}
return data;
}
The pName is set with:
string uri = "http://MyWebSiteName.com";
When the user authenticate the first time, I call the WriteToCookie function and set it with 7 days to expire.
It looks like everything is fine as I get no exception of error messages. (I have a break point in the catch)
After that, I close the session and start it again.
The first thing I do is a ReadFromCookie.
Then I get an exception with the following message: No more data is available
So my application is sending the user automatically back to the login screen.
I also tried to do a ReadFromCookie right after the WriteToCookie in the same session, and I get the same error.
Application.SetCookie(new Uri("http://MyWebSiteName.com/WpfBrowserApplication1.xbap"), "Hellllo");
string myCookie2 = Application.GetCookie(new Uri("http://MyWebSiteName.com/WpfBrowserApplication1.xbap"));
It seems to me that the cookie is not even written in the first place.
So I am guessing I am doing something wrong.
Maybe the uri I am using is wrong. Is there a specific format needed for it?
Just like you need a very specific format for the expire date.
I have been searching quite a lot of internet for a good sample/tutorial about using cookies with XBAP, and I could not find anything really well documented or tested.
A lot of people say that it works, but no real sample to try.
A lot of people also handle the authentication in html, then go to the XBAP after successfully reading/writing the cookies.
I would prefer a full XBAP solution if possible.
To answer some questions before they are asked, here are the project settings:
Debug:
Command line arguments: -debug -debugSecurityZoneURL http://MyWebSiteName.com "C:\Work\MyWebSiteName\MyWebSiteNameXBAP\bin\Debug\MyWebSiteNameXBAP.xbap"
Security:
Enable ClickOnce security settings (Checked)
This is a full trust application (selected)
I also created a certificate, and added it the 3 stores like explained in "publisher cannot be verified" message displayed
So I do not have the warning popup anymore. I just wanted to make sure that it was not a permission issue.
Finally found the answer to this problem.
Thanks for this CodeProject I was finally able to write/read cookies from the XBAP code.
As I had guessed, the URI needs to be very specific and you cannot pass everything you want in it.
What did the trick was using: BrowserInteropHelper.Source
In the end the read/write code looks like:
Application.SetCookie(BrowserInteropHelper.Source, data);
string myCookie = Application.GetCookie(BrowserInteropHelper.Source);
It looks like you cannot use ';' to separate your own data.
If you do so, you will only get the first entry in your data.
Use a different separator (ex: ':') and then you can get everything back
The data look like this:
n=something:k=somethingElse;expires=Tue, 12 May 2015 14:18:56 GMT ;
The only thing I do not get back from Application.GetCookie is the expire date.
Not sure if it is normal or not. Maybe it is flushed out automatically for some reason. If someone knows why, I would appreciate a comment to enlighten me.
At least now I can read/write data to the cookie in XBAP. Yeah!

email issue with spring and java mail api

My application is now running in websphere 6.1 in AIX box, in which there is a email notification module which works fine till today. The same application is now getting migrated to a Tomcat based cloud platform which is running in a Linux server. The emails are getting sent out from the new Linux based platform to intended recipients, but the email is looking weird with no subject, no recipient email address, attachments are not proper, etc.
Previously it is ant-based build and now i build the project with maven. And i tried to keep same versions of spring and mail api jars.
What could be the issue and what am I doing wrong here? There are no exceptions while sending the email and it is really hard to identify the issue. Appreciate your help and Thanks in advance.
code:
public synchronized void sendwithAttachment(MailMessage mailMessage) throws MailException, MessagingException {
String methodName = "sendwithAttachment";
LogManager.info(LogConstants.NOTIFICATION,
Constants.ENTERING_METHOD + methodName, this.getClass());
try{
MailSender MailSender = (MailSender) mailSender;
Session session = MailSender.getSession();
MimeMessageHelper message = new MimeMessageHelper(new MimeMessage(session), true, "UTF-8");
message.setFrom(mailMessage.getFrom());
message.setTo(mailMessage.getTo());
message.setSubject(mailMessage.getSubject());
message.setSentDate(new Date());
message.setText(mailMessage.getText());
DiskFileAttacher[] diskFileAttachments = mailMessage.getDiskFileAttachments();
for (int i = 0; ((diskFileAttachments != null) && i < diskFileAttachments.length); i++) {
message.addAttachment(diskFileAttachments[i].getAttachmentFilename(), new File(diskFileAttachments[i].getFilePath()));
}
InMemoryFileAttacher[] inMemoryFileAttachments = mailMessage.getInMemoryFileAttachments();
for (int i = 0; ((inMemoryFileAttachments != null) && i < inMemoryFileAttachments.length); i++) {
if(inMemoryFileAttachments[i].getFileObj()!=null){
message.addAttachment(inMemoryFileAttachments[i].getFileName(),
inMemoryFileAttachments[i].getFileObj() );
}else{
message.addAttachment(inMemoryFileAttachments[i].getFileName(),
inMemoryFileAttachments[i]);
}
}
List mimeMessageList = new ArrayList();
mimeMessageList.add(message.getMimeMessage());
MimeMessage[] mimeMessages = (MimeMessage[]) mimeMessageList.toArray(new MimeMessage[mimeMessageList.size()]);
LogManager.info(LogConstants.NOTIFICATION,"before MailSender.send is called " + methodName, this.getClass());
MailSender.send(mimeMessages);
}catch(MailException e){
LogManager.info(LogConstants.NOTIFICATION, "Some exception occured while sending emails...", this.getClass());
}
Check which version of JavaMail you're using. If you're including the JavaMail jar file in your application, there may also be a copy of the GNU version of JavaMail on your Linux system that's being used instead of yours. Look for the libgnumail-java package.
I found the issue and resolved this. I include maven dependency for axiom jars, which has also dependency for "geronimo-javamail_1.4_spec" and this geronimo java mail jars are also downloaded. And while sending emails this geronimo mail api was used instead of original java mail api. I added exclusions and now it is resolved.

Clickonce Autodetect Proxy

I have created a C sharp Wpf ClickOnce application which uses xml rpc for communincation. A lot of my users get there proxy settings in different ways. Some use a pac file, other from IE or dhcp etc. I want to automate this whole process of getting the proxy details in any kind of environment. I have tried a LOT of different code snippets but want to hear if something like this already exists.
I see the Xml Rpc documentation has a setProxy method but I'm not sure how to specify the username or passsword if one is used. This whole process is still a little bit confusing for me.
I have also tried many different pieces of code including the WebProxy Class and using DefaultCredentials,DefaultProxy,GetSystemWebProxy etc.
At the moment I'm going to try a dllimport using winhttp to get the proxy settings. I am not sure if one can do this in a Clickonce Deployment. Is the dllimport the same as p/invoke ?
As you can see I would appreciate some advice on how to go about getting ANY type of proxy setting.
Appreciate any feedback.
ClickOnce installation/update doesn't really support proxy authentication. It will use the information in IE, and sometimes the machine.config file. The definitive thread with all known information about this is here.
I haven't had have problems with proxy authentication from the standpoint of installing applications. When using our application, which called backend WCF services, we let the user provide his proxy authentication information, and we applied the settings programmatically when making the service calls. This has nothing to do with ClickOnce.
This worked for me :
public static IExample ProxyAndCredentials { get; set; }
public static string ProxyUrl { get; set; }
public static void SetupProxyAndCredentials() {
//Insert your website here where XmlRpc calls should go
var url = new Uri("http://www.example.com/");
try
{
ProxyUrl = WebRequest.DefaultWebProxy.GetProxy(url).ToString();
Log.Debug(url + " is using proxy " + ProxyUrl);
if (ProxyUrl == url.ToString() || ProxyUrl == url + "/"){
// A proxy is not in use here
ProxyUrl = "";
Log.Debug("No proxy is used for " + url);
}
else if (!String.IsNullOrEmpty(ProxyUrl)){
// A proxy is in use
ProxyAndCredentials.Proxy = new WebProxy(ProxyUrl);
Log.Debug("A proxy is used for " + url);
}
//Set credentials, in my experience it is better to always set these
ProxyAndCredentials.Credentials = CredentialCache.DefaultNetworkCredentials;
ProxyAndCredentials.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
}
catch (Exception p)
{
//Handle Exception
}
}

javamail ignoring socks proxy

I've setup javamail to run through a socks proxy, but it won't go through it. I tried setting both mail.smtp.socks.host and mail.smtp.socks.port but it keeps going direct to the mail host. I feel like I'm missing something very basic. any help would be appreciated. Here's my properties:
props.setProperty("mail.smtp.host", this.smtpHost);
props.setProperty("mail.smtp.port", Integer.toString(this.smtpPort));
props.setProperty("mail.smtp.user", this.smtpUser);
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.starttls.enable", Boolean.toString(this.smtpTLS));
props.setProperty("mail.debug", "true");
if (this.useSOCKSProxy) {
logger.info("Using proxy");
props.setProperty("mail.smtp.socks.host", "127.0.0.1");
props.setProperty("mail.smtp.socks.port", Integer.toString(1080));
props.setProperty("mail.smtps.socks.host", this.socksProxyHost);
props.setProperty("mail.smtps.socks.port", Integer.toString(this.socksProxyPort));
props.setProperty("proxySet", "true");
props.setProperty("socksProxyHost", this.socksProxyHost);
props.setProperty("socksProxyPort", Integer.toString(this.socksProxyPort));
}
//Session session = Session.getInstance(props, new SmtpAuthenticator(this.smtpUser,this.smtpPassword));
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUser, smtpPassword);
}
});
//Session session = Session.getInstance(props, null);
session.setDebugOut(System.out);
session.setDebug(true);
//Transport tr = session.getTransport("smtp");
//tr.connect();
//tr.connect(this.smtpHost,this.smtpPort, this.smtpUser, this.smtpPassword);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(this.smtpFrom));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
msg.setSubject(subject);
msg.setText(msgToSend.toString());
msg.saveChanges();
Transport.send(msg);
Start by fixing these common mistakes.
If that doesn't solve the problem, post the debugging output. Set the System property "mail.socket.debug" to "true" to get additional debugging output.
What version of JavaMail are you using? What JDK?
Unless "props" is coming from System.getProperties(), setting "socksProxyHost" has no effect. And if you do set it as a System property, you don't need the JavaMail socks properties.
You can try to figure out the correct combination of properties, including the common mistakes Bill mentioned, or you can also go the easy way and let Simple Java Mail (open source) figure it out for you:
Your example converted:
Mailer mailer = new Mailer(
new ServerConfig(smtpHost, smtpPort, smtpUser, smtpPassword),
TransportStrategy.SMTP_TLS,
this.useSOCKSProxy ? new ProxyConfig(socksProxyHost, socksProxyPort) : null
);
mailer.setDebug(true);
mailer.sendMail(new EmailBuilder()
.from(null, smtpForm)
.to(null, email)
.subject(subject)
.text(msgToSend.toString())
.build());
Note how you don't have to deal with any properties or JavaMail API anymore. You can also use authenticated proxy if you need.

Exchange Web Services (EWS) API "To" header for alias

I have an inbox set up in exchange, hello#mycompany.com
Additionally, there is an alias for this, news#mycompany.com, so all emails to the news address end up in the hello inbox.
Ideally, I want to be able to tell which alias an email has been sent to, using EWS.
When I send an email to news#mycompany.com, and examine the Internet headers of the message using Microsoft Outlook, the To: header reads To: Hello <news#mycompany.com> which is exactly what I want to see.
However, using EWS, when I look at the ToRecipients property of the message, the reported email address is always that of the primary SMTP address. Also the InternetMessageHeaders property of the Webservices.Data.Item does not contain the To: property. I also can't seem to see the correct address using EWSEditor to examine all the properties of the message.
The answer to this forum post seems to suggest that,
...The Information about the actual email address a message is sent to is stored in the recipients collection which you can't access (outside of exportmessage) in EWS...
How would I go about doing this programatically so I can find the correct To: address?
This works for me:
private static string GetToAddress()
{
ExchangeService exService = new ExchangeService();
exService.Credentials = new NetworkCredential("username", "password", "domain");
exService.Url = new Uri("https://youraddress/EWS/Exchange.asmx");
ExtendedPropertyDefinition PR_TRANSPORT_MESSAGE_HEADERS = new ExtendedPropertyDefinition(0x007D,MapiPropertyType.String);
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties)
{PR_TRANSPORT_MESSAGE_HEADERS, ItemSchema.MimeContent};
FindItemsResults<Item> fiResults = exService.FindItems(WellKnownFolderName.Inbox, new ItemView(1));
foreach (Item itItem in fiResults.Items)
{
itItem.Load(psPropSet);
Object valHeaders;
if (itItem.TryGetProperty(PR_TRANSPORT_MESSAGE_HEADERS, out valHeaders))
{
Regex regex = new Regex(#"To:.*<(.+)>");
Match match = regex.Match(valHeaders.ToString());
if (match.Groups.Count == 2)
return match.Groups[1].Value;
}
return ToAddress;
}
return "Cannot find ToAddress";
}
The code is from:
http://social.technet.microsoft.com/Forums/en-au/exchangesvrdevelopment/thread/1e5bbde0-218e-466e-afcc-cb60bc2ba692

Resources