How to get server message? - c#

If you try to visit ftp.yealink.com you will get a MessageBox saying:
The Server ftp://ftp.yealink.com:21 requires a username and a password.
How do I receive the above text using C#?
Can it be done through the WebClient class?
Edit: I think some of you misunderstood me. I am not looking to connect to the FTP server, or handle the exception. All I want is a string with the value: "The Server ftp://ftp.yealink.com:21 requires a username and a password."

If you use the WebClient class to connect to an ftp site, you need to create a NetworkCredentials instance to store your login info (username, password):
var client = new WebClient { Credentials = new NetworkCredential("username", "password") };
try
{
var data = client.DownloadData(<some uri to a file path on the ftp site>);
...
}
catch (WebException e)
{
// handle any exceptions
}
More info can be found here: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx
EDIT:
You clarified that you're looking for the popup text shown in your browser when clicking on the ftp link. The text string of "The Server ... requires a username and a password" is generated by your browser (Chrome). Firefox pops up a message box stating "Enter username and password for ..." and Safari says "Enter your name and password for the server ..." The text you're trying to capture isn't output by the ftp server.

When connecting you will receive a WebException with StatusCode 421. This means Not authorized. Handle the exception and retry.
Given the comments you place I think you are looking for the WelcomeMessage and BannerMessage from the response.

Related

Counting Emails in inbox from SSIS (C#) - The remote server returned an error: (401) Unauthorized

I am trying to create a connection to the ExchangeService from a script component (C#) in SSIS to be able to count the total number of emails in an inbox.
My error comes when I try to use the connection - so far I've been testing the connection to simply try and send a test email to myself and its on msg.send() that I get the error:
The remote server returned an error: (401) Unauthorized
I have been using the WebServices (EWS) NuGet Package with the code below:
ExchangeService exService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
string username = "emailUser";
string pwd = "MyPwd";
string domain = "NetBIOS_DOMAIN";
exService.Credentials = new WebCredentials(username, pwd, domain);
exService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
EmailMessage msg = new EmailMessage(exService)
{
Subject = "This is a test!!!",
Body = "This is the message body"
};
msg.ToRecipients.Add("emailUser#domain.co.uk");
msg.Send();
I've not got as far as looking into inboxes yet because as soon as I try to bind a folder I get the same error so figured this was an easier test.
I have tried both WebCredentials() and NetworkCredential() - both give the same error.
I have been primarily following the notes from here
A search on the error led me to try all manor of variances with the username/domain as outlined in an answer here
And I have tested my remote connectivity with this tool which gives the same error (Screenshot)
I am testing this with my own credentials at present but eventually it would be replaced with a different used or a service account. I think everything is pointing at me entering them wrong somehow but I'm 2 days in and totally stumped.
Thanks all

C# connect to remote server using Microsoft Terminal Services Active Client (RDP)

I have a piece of code that should connect to the server. The code is as following:
var rdp = new MsRdpClient8NotSafeForScripting();
rdp.Server = "192.168.0.101"; //adress
rdp.Domain = "localdomain"; //domain
rdp.UserName = "test"; //login
rdp.AdvancedSettings8.ClearTextPassword = "123456";//password
try
{
rdp.Connect();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine(rdp.Connected);
if (rdp.Connected != 0)
{
rdp.Disconnect();
}
Console.ReadLine();
This is supposed to "connect" to my remote server via 3389 port so that I can be able to read a file from my desktop which is called: "min.txt"
So far I have tried specifying the login data of my server but I always just get output of "0" in the console's window, regardless of whether I specify correct or incorrect login data..
My questions here are:
Why is it connecting even with wrong login data (ip, user + password)
How can I, once I've been indeed successfully connected to the server, access the min.txt file on my remote server, which is located at desktop...
Can someone help me out?
Probably you can try specifying the password like below:
MSTSClib.IMsTscNonScriptable secured = (MSTSClib.IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = “123456”;
For reference: MSDN link is here
Once connected, you can access the file like a shared network file via UNC.
Example:
System.IO.FileStream stream = System.IO.File.OpenRead("\\servername\sharedname\path\somefile.txt");
Then need to ensure that permissions are in place to access the folder.

I have an exception in sending email using c#

I create a method for send Email like:
public async Task SendEmailCC(string body, string subject, List<string> mainRecievers, List<string> receivers)
{
SmtpClient client = new SmtpClient("smtp-mail.outlook.com")
{
UseDefaultCredentials = true,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("Test#gmail.com", "MyMailPassword")
};
MailMessage mailMessage = new MailMessage { From = new MailAddress("job#test.org") };
foreach (var reciever in mainRecievers)
{
mailMessage.To.Add(reciever);
}
foreach (var item in receivers)
{
mailMessage.CC.Add(item);
}
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.Subject = subject;
await client.SendMailAsync(mailMessage);
}
For test, I send an email to my self, but I didn't get Email. how can I find my problem?
My exception is connection auth.
Mail server was google and I used of OutLookSmtp,To this section of my code I have that and I changed. like:
SmtpClient client = new SmtpClient("smtp-mail.outlook.com")
And changed:
SmtpClient client = new SmtpClient("smtp.gmail.com")
After that I get this exception:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required", than the error might occur due to following cases.
For this issue I try this:
case 1: when the password is wrong
case 2: when you try to login from some App
case 3: when you try to login from the domain other than your time zone/domain/computer (This is the case in most of scenarios when sending mail from code)
There is a solution for each
solution for case 1: Enter the correct password.
solution 1 for case 2: go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps . So that you will be able to login from all apps.
solution 2 for case 2:(see https://stackoverflow.com/a/9572958/52277) enable two-factor authentication (aka two-step verification) , and then generate an application-specific password. Use that newly generated password to authenticate via SMTP.
solution 1 for case 3: (This might be helpful) you need to review the activity. but reviewing the activity will not be helpful due to latest security standards the link will not be useful. So try the below case.
solution 2 for case 3: If you have hosted your code somewhere on production server and if you have access to the production server, than take remote desktop connection to the production server and try to login once from the browser of the production server. This will add excpetioon for login to google and you will be allowed to login from code.
But what if you don't have access to the production server. try the solution 3
solution 3 for case 3: You have to enable login from other timezone / ip for your google account.
to do this follow the link https://g.co/allowaccess and allow access by clicking the continue button.
And that's it. Here you go. Now you will be able to login from any of the computer and by any means of app to your google account

Does a password need encoding when client authenticates using OpenPop?

I'm using openpop to connect to gmail pop server.
This is the code (very simple)
using (var client = new Pop3Client())
{
var username = mailProperties.UserId;
client.Connect(mailProperties.Server, mailProperties.Port, mailProperties.UseSsl);
client.Authenticate(username, mailProperties.Password);
}
Most of the time it works. But if the password contains "./", the authentication fails and I have an Invalid login exception.
Do I have to encode the password or something? I tried Uri.EscapeDataString but the result is the same.
I verified the password and it's the good one ;)
I forgot to activate the pop access in my gmail option for this account.
The problem is now solved.

Selenium Webdriver automation with emails

I'm currently trying to use Selenium Webdriver (C#) to automate a Forgot Password -> Reset Password workflow, where the user navigates to a page and supplies their username, and the backend code validates the username, then sends an email with a reset password link to the email address associated with their account.
I'm able to automate the process up to the point where the code sends the email, but I don't know any ways of checking for the email and/or clicking a link in the email, so I was hoping someone more experienced with Selenium/automation may be able to give me a few pointers.
Ideally the test should not care about the email address that the email is being sent to. Is there a way for Selenium WebDriver or some 3rd party package to catch the email being sent?
Thanks for any input or suggestions.
No. You are talking about setting up an email server, which is not an easy task.
You should send it to a test work email (if this is for a company), or a public email (hotmail/gmail), or if security is not an issue at all, the easiest place to send it would be a disposable email (mailinator)
You could try PutsBox. You can send an email to whatever-you-want#putsbox.com, wait for a few seconds (SMTP stuff ins't instantaneous) then check your email via http://preview.putsbox.com/p/whatever-you-want/last.
Have a look at this post tutorial, it can give you some ideas.
There is no integration of selenium with email clients like Thunderbird/Outlook. But if you have a web interface of the same email client, then you can access the email from browser and using selenium you can read and respond to the emails. I have tried this recently and it works fine where I have used web Outlook for testing.
Hope this helps.
Hi I was in a similar situation and was able to successfully implement a way to get an activation or forgotten password links.
Using Java Mail API I was able to trigger a method when such action is performed which goes into a Folder and read a specific message line then get the link and open it up in a browser using WebDriver.
However the main drawback with this is the inconsistency of reading a specific folder, sometimes emails goes to spam or other folder (in case of Gmail the new Social Folder) making it invisible or difficult to be retrieved.
Overall i think its a process that shouldn't really be automated, In terms of testing it should be done more code base level by mocking responses.
Snippet below should give you an idea on how to go about implementing
public class RefactoredMail {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "username", "password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
System.out.println("Activation Link:" + ((String)
bp.getContent()).startsWith("http"));
String [] line = new String[1];
line [0] = mp.getContentType().toString();
System.out.println("Activation Link:" + (mp.getBodyPart(0).getLineCount()));
System.out.println("Activation Link:" +line[0]);
} catch (Exception e) {
e.printStackTrace();
}
//WebDriver Stuffs
public String activationUrl() {
//getting the url link and making it a global variable .... etc
//Accessing the link
}
}
You can use https://github.com/cmendible/netDumbster or http://ndumbster.sourceforge.net/default.html. I've used one i forget which. This will host an smtp listener and allow you to make assertions against any email it receives. Its kind of awesome! The caveat is you need to be able to control where the server delivers mail in the environment you are testing.

Categories