Tag Archive for csharp

Send Email from your GMAIL account using ASP.Net and C#

using System.Web.Mail;
using System;
public class MailSender
{
public static bool SendEmail(
string pGmailEmail,
string pGmailPassword,
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
try
{
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserver",
"smtp.gmail.com");
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"465");
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusing",
"2");
//sendusing: cdoSendUsingPort, value 2, for sending the message using
//the network.

//smtpauthenticate: Specifies the mechanism used when authenticating
//to an SMTP
//service over the network. Possible values are:
//- cdoAnonymous, value 0. Do not authenticate.
//- cdoBasic, value 1. Use basic clear-text authentication.
//When using this option you have to provide the user name and password
//through the sendusername and sendpassword fields.
//- cdoNTLM, value 2. The current process security context is used to
// authenticate with the service.
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
//Use 0 for anonymous
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusername",
pGmailEmail);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendpassword",
pGmailPassword);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
"true");
myMail.From = pGmailEmail;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim() != "")
{
MailAttachment MyAttachment =
new MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;
}

System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}

//Example usage of this class

EmailLib.SendEmail.SendEmail("your_gmail_id",
"your_gmail_password",
"to_email@anydomain.com",
"This is email subject" ,
"This is email body",
Web.Mail.MailFormat.Text,
"Physical path to your Attachment")

source

CSharp Reading String from Resource File .

/*
The resource file stores items as name-value pair.

To get string values from the resource files (.resx) added in your project, use the following code.
*/

//Name Spaces Required
using System.Resources;
using System.Reflection;

// Create the resource manager.
Assembly assembly = this.GetType().Assembly;

//ResFile.Strings -> <Namespace>.<ResourceFileName i.e. Strings.resx>
resman = new ResourceManager("StringResources.Strings", assembly);

// Load the value of string value for Client
strClientName = resman.GetString("Client");

source

Regular Expression to Grab an Object Type Name from a C# Code File.

(?<=news)s*S*?s*(?=[[(])

source

Regular Expression to Grab an Object Reference Name from a C# Code File.

S*s*(?==s*S*s*news)

source

Regular Expression to Grab a Method Name from a C# Code File.

S*s*(?=()

source

Regular Expression to Grab a ClassName from a C# Code File

(?<=classs)s*S*

source

Python – getFile Internet

package get.file.example;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class GetFileExample
{
public GetFileExample()
{
try
{
byte[] bite = new byte[2048];
int read;
URL url = new URL("http://switch.dl.sourceforge.net/sourceforge/pys60miniapps/FlickrS60_src_v0.1b.tar.bz2");

BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fos = new FileOutputStream("/tmp/file.tar.bz2");

while((read = bis.read(bite))>0)
{
fos.write(bite, 0, read);
System.out.println("--");
}

fos.close();
bis.close();

System.out.println("FINE");

}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
new GetFileExample();
}
}

source